您好,登錄后才能下訂單哦!
在Android項目中常常會有需要TextView顯示很長文本的時候,這時候TextView的換行策略就成了一大問題,但是如何使TextView執(zhí)行自己想要的換行效果呢。TextView的屬性中有幾點是可以起到作用的。
(1).正常顯示
xml代碼
<TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="這是一句很長的話|這是一句很長的話|這是一句很長的話|這是一句很長的話|這是一句很長的話|" android:textAppearance="?android:attr/textAppearanceLarge" />
顯示結(jié)果
可以看到這句話占了3行,并且是由系統(tǒng)自己判斷一行的剩余寬度能否再放下一個字符進行換行的。
(2).使用singleLine屬性
xml代碼
<TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:singleLine="true" android:text="這是一句很長的話|這是一句很長的話|這是一句很長的話|這是一句很長的話|這是一句很長的話|" android:textAppearance="?android:attr/textAppearanceLarge" />
顯示結(jié)果
可以看到這時不管有多長的一段話都會在一行內(nèi)顯示出來,超過一行的部分使用...表示。
那若這時顯示的字數(shù)不足一行時會怎么樣呢。
xml代碼
<TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:singleLine="true" android:text="這是一句很長的話|" android:textAppearance="?android:attr/textAppearanceLarge" />
顯示結(jié)果
這時并不會出現(xiàn)...的情況。
若想嚴格控制TextView的行數(shù)在一行之內(nèi)的可以使用這一屬性。
(3).使用lines屬性
實際行數(shù)3>lines屬性值2
xml代碼
<TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:lines="2" android:text="這是一句很長的話|這是一句很長的話|這是一句很長的話|這是一句很長的話|這是一句很長的話|" android:textAppearance="?android:attr/textAppearanceLarge" />
顯示結(jié)果
這次加了一個lines的屬性,控制顯示的行數(shù)為2,可以看到,顯示的行數(shù)確實是2行,但是超出部分并不會如singLine那樣有...的提示,
并且這個屬性還有一個局限處,下面來看看,我們已經(jīng)知道正常顯示時是3行,可如果我們把lines的屬性值定為5會如何呢
實際行數(shù)3<lines屬性值5
xml代碼
<TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:lines="5" android:text="這是一句很長的話|這是一句很長的話|這是一句很長的話|這是一句很長的話|這是一句很長的話|" android:background="#888888" android:textAppearance="?android:attr/textAppearanceLarge" />
顯示結(jié)果
為了顯示明顯,這次在TextView上加上了背景色,可以看出來,不管這些文本實際占了多少行,都會按照lines給出的數(shù)值去占位。
若不能確定這些文本究竟占了多少行,不要輕易使用lines屬性,若要使用,則這個值宜小不宜大。
(3).使用maxLines屬性
實際行數(shù)3<最大行數(shù)5
xml代碼
<TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:maxLines="5" android:text="這是一句很長的話|這是一句很長的話|這是一句很長的話|這是一句很長的話|這是一句很長的話|" android:background="#888888" android:textAppearance="?android:attr/textAppearanceLarge" />
顯示結(jié)果
設(shè)置了最大行數(shù)為5行,但是這個屬性會根據(jù)實際顯示進行縮減,僅顯示實際內(nèi)容所占的行數(shù)。
若實際行數(shù)3>最大行數(shù)2的情況呢?
xml代碼
<TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:maxLines="2" android:text="這是一句很長的話|這是一句很長的話|這是一句很長的話|這是一句很長的話|這是一句很長的話|" android:background="#888888" android:textAppearance="?android:attr/textAppearanceLarge" />
顯示結(jié)果
可以看到顯示結(jié)果很好的執(zhí)行了我們設(shè)置的最大行數(shù)屬性,只顯示出了兩行,雖然也沒有如singLine一樣給出...的還有內(nèi)容的提示。
個人認為這是對于控制多行顯示中最好的方式。
(4).自主控制換行
若內(nèi)容自己知道,并且需要自行決定在什么位置換行,可以常用在文本中添加"\n"的方式進行換行
xml代碼
<TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="這是一句很長的話|\n這是一句很長的話|\n這是一句很長的話|\n這是一句很長的話|\n這是一句很長的話|" android:background="#888888" android:textAppearance="?android:attr/textAppearanceLarge" />
顯示結(jié)果
這樣實現(xiàn)了自己決定何時換行。
綜上所述,
(1).對于文本內(nèi)容未知的情況,想要控制行數(shù)并可以最優(yōu)的顯示出文本內(nèi)容,當采用maxLines屬性。
(2).對于文本內(nèi)容已知的情況,直接使用"\n"進行手動換行即可。
(3).以上的兩種情況,只想單行顯示時,采用singLine屬性為最佳。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。