Android 如何将TextView限制为整行
我在ListView中有一个单元格,布局如下 问题是文本视图中最后一行有黑色文本。我希望文本视图中只有完整的行(本例中有两行),最好使用省略号来表示文本已被删除 在这个特定的例子中,我可以只设置maxLines=“2”,但有时粉色标题可能只有一行,因此在这些情况下,我希望主体有足够的空间容纳完整的三行 编辑:显示XMLAndroid 如何将TextView限制为整行,android,android-layout,layout,Android,Android Layout,Layout,我在ListView中有一个单元格,布局如下 问题是文本视图中最后一行有黑色文本。我希望文本视图中只有完整的行(本例中有两行),最好使用省略号来表示文本已被删除 在这个特定的例子中,我可以只设置maxLines=“2”,但有时粉色标题可能只有一行,因此在这些情况下,我希望主体有足够的空间容纳完整的三行 编辑:显示XML <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://s
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="110dp">
<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/card_background">
<ImageView
android:id="@+id/imgNews"
android:layout_marginLeft="16dp"
android:layout_centerVertical="true"
android:layout_width="65dp"
android:layout_height="65dp"
android:scaleType="centerCrop"
tools:src="@drawable/news1" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/imgNews"
android:layout_centerVertical="true"
android:layout_marginTop="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="8dp">
<TextView
android:id="@+id/lblNewsDate"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:textSize="12sp"
android:textColor="#b3b3b3b3"
android:maxLines="1"
tools:text="29. oktober 2015" />
<TextView
android:id="@+id/lblNewsTitle"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:textSize="14sp"
android:textColor="#df0784"
android:textAllCaps="true"
android:maxLines="2"
android:ellipsize="end"
tools:text="Festivalen modtager donationer fra fionia fond"/>
<TextView
android:id="@+id/lblNewsDescription"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:textSize="13sp"
android:textColor="#000000"
android:ellipsize="end"
tools:text="H.C. Andersen Festivals åbner nu for ansøgninger om projekter til festivalen i 2016. Festivalen leder blandt andet efter gadegøgl, teater, dans, musik, foredrag, installationer og kunst, som kan være med til at skabe et forunderligt og overraskende univers i uge 34 i Odense."/>
</LinearLayout>
</RelativeLayout>
</FrameLayout>
如果你知道你总是能够显示两行(这取决于你的布局如何调整自己),你可以将最大行数设置为两行,也可以设置省略号,以便在剪切消息后修剪字符串并放入“…” 在xml中的Textview字段中:
android:maxLines = "2"
android:ellipsize = "end"
建议:为什么不使用两种不同的文本视图?一个用于标题,另一个用于内容。显示您正在使用的适配器布局我知道我可以使用maxLines和ellipsize。问题是有时有三条线的空间。当出现这种情况时,我想显示三行。您可以向我们显示您的xml布局吗?它会更快!!绝对高度(如110dp)是最重要的。我不知道你是否值得处理绝对尺寸(显然这取决于你的项目和设计)。我总是尝试使用包裹内容或匹配父项,而不是不太重要的权重(用于线性布局)。包装内容将使容器适应您的内容。如果使用包裹内容设置容器,将LinearLayout居中,并将maxLines设置为3,则容器的两行或三行之间的间距差应适当。这是我的建议,在不做太多更改的情况下分析您的代码。。。我希望这会有所帮助。不幸的是,设计确实要求电池具有相同的高度。我目前的解决方案是使用固定单元格高度的maxLines=“2”。看起来还不错,但是如果我能把风景看得尽善尽美的话,我会喜欢的。哦,好的。设计规则哈哈。对不起,如果我不能帮你的话。