我有一个 TextView,我必须在其中加载一条消息。 TextView 最多有 2 行 (android:lines="2"
)。该消息可能包含“\n”字符。
我应该如何在该 TextView 中加载消息以便包装单词,如果这两行消息不适合,在最后一个可见单词的末尾我必须添加三个点 (...)?如何检测适合该 TextView 的文本长度?
我的 TextView 代码是
<TextView
a:id="@+id/tv_message"
a:gravity="top"
a:layout_width="wrap_content"
a:layout_height="wrap_content"
a:layout_alignParentTop="true"
a:layout_toRightOf="@id/iv_icon"
a:layout_marginLeft="2dp"
a:layout_marginTop="4dp"
a:paddingRight="7dp"
a:paddingBottom="5dp"
a:textSize="12sp"
a:typeface="sans"
a:ellipsize="marquee"
a:lines="2"
a:maxLines="2"
a:textColor="@android:color/black"
/>
但在应用程序中,文本出现在两行上,即使有一行包含签名。消息类似于:“消息文本”+“\nSignature”,但消息可以是 1、2、3 行,具体取决于消息长度。
最佳答案
在 XML 中,为 TextView 使用属性 android:ellipsize="marquee"
。
你的 TextView 可以这样定义:
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="2"
android:lines="2"
android:ellipsize="marquee"/>
编辑:我尝试了以下代码,改编自您的:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rlt_news"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/cell_background"
android:padding="5dip" >
<TextView
android:id="@+id/tv_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:paddingLeft="5dip"
android:text="Name"
android:textStyle="bold" />
<ImageView
android:id="@+id/rss_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_title"
android:layout_marginLeft="2dip"
android:adjustViewBounds="true"
android:maxHeight="100dip"
android:maxWidth="100dip"
android:src="@drawable/rss_cell_icon"
android:visibility="visible" />
<TextView
android:id="@+id/tv_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="2dp"
android:layout_marginTop="4dp"
android:layout_toRightOf="@id/rss_icon"
android:ellipsize="marquee"
android:gravity="top"
android:lines="2"
android:maxLines="2"
android:paddingBottom="5dp"
android:paddingRight="7dp"
android:text="Description of the news, just the short paragraph, \nSignature, to see how it works if there are more than two lines"
android:textColor="@android:color/black"
android:textSize="12sp"
android:typeface="sans" />
</RelativeLayout>
这是输出:
关于android - Textview 显示部分文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4860020/