如何在 ListView 行中显示较长的 TextView 高度?
我有一个如下所示的 ListView:
注意 TextView 如何被省略号截断。如何确保整个 TextView 在 ListView 内可见?
这是该行的 XML(文本视图称为 bodyTextView
):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="4px">
<ImageView android:id="@+id/avatarImageView"
android:layout_width="48px"
android:layout_height="48px"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="4px">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:gravity="center_vertical">
<TextView android:id="@+id/usernameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:textStyle="bold"
android:singleLine="true"
android:ellipsize="end"
android:textColor="#444444"
android:padding="0px"/>
<TextView android:id="@+id/bodyTextView"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false"
android:textColor="#666666"
android:maxLines="5"
android:ellipsize="end"/>
<TextView android:id="@+id/dateTextView"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
I have a ListView that looks like this:
Notice how the TextView is getting cut off with elipses. How can I make sure the entire TextView is visible inside of the ListView?
Here is my XML for the row (the textview is called bodyTextView
):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="4px">
<ImageView android:id="@+id/avatarImageView"
android:layout_width="48px"
android:layout_height="48px"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="4px">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:gravity="center_vertical">
<TextView android:id="@+id/usernameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:textStyle="bold"
android:singleLine="true"
android:ellipsize="end"
android:textColor="#444444"
android:padding="0px"/>
<TextView android:id="@+id/bodyTextView"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false"
android:textColor="#666666"
android:maxLines="5"
android:ellipsize="end"/>
<TextView android:id="@+id/dateTextView"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
设置椭圆大小=“无”。另外,请确保指定足够大的 maxLines 。
Set ellipsize="none". Also, be sure to specify maxLines big enough.
实际上, ellipsize="none" 不会影响元素的宽度。
要让某个元素在 LinearLayout 中显示其完整内容,您需要将其layout_width或layout_height(以适用者为准)设置为“wrap_content”,然后确保LinearLayout中的其他元素不会首先占用该空间。当然,即使使用此选项,单行 TextView 在填充周围布局的宽度后仍然会变成椭圆形。
你如何确保其他元素发挥良好?确保它们没有也设置为 fill_* 选项或wrap_content。显示多个 TextView 的一种简单方法是:将第一个 TextView 设置为“wrap_content”的布局宽度和“0”的布局权重(这是默认权重,您可以将其设置为“0”)。不需要设置),然后将其他设置为layout_width为“0dip”,layout_weight为“1”。这将为布局提供与内容匹配的设置宽度的主 TextView,以及有效宽度为零的其他 TextView。然后布局将查看layout_weights并调整其他TextView的大小以占用剩余空间。
Actually, ellipsize="none" won't affect the width of the element.
To get an element to display its full contents in a LinearLayout, you need to set its layout_width or layout_height (whichever is applicable) to "wrap_content", and then ensure that other elements in the LinearLayout will not take up the space first. Even with this option, a singleLine TextView will still ellipsize once it fills the width of the surrounding layout, of course.
How do you make sure other elements play nice? Make sure they are not also set to a fill_* option or wrap_content. An easy way to display several TextViews with one taking up all the space it needs and the others playing nice is this: Set the primary one to a layout_width of "wrap_content" with a layout_weight of "0" (this is the default weight, you do not need to set it), and then set the others to a layout_width of "0dip" with a layout_weight of "1". This will give the primary TextView to the layout with a set width matching the content, and the other TextViews with an effective width of zero. The layout will then see the layout_weights and resize the other TextViews to take up the remaining space.