在Android項(xiàng)目中常常會(huì)有需要TextView顯示很長(zhǎng)文本的時(shí)候,這時(shí)候TextView的換行策略就成了一大問(wèn)題,但是如何使TextView執(zhí)行自己想要的換行效果呢。TextView的屬性中有幾點(diǎn)是可以起到作用的。
從策劃到設(shè)計(jì)制作,每一步都追求做到細(xì)膩,制作可持續(xù)發(fā)展的企業(yè)網(wǎng)站。為客戶提供網(wǎng)站設(shè)計(jì)、做網(wǎng)站、網(wǎng)站策劃、網(wǎng)頁(yè)設(shè)計(jì)、主機(jī)域名、虛擬主機(jī)、網(wǎng)絡(luò)營(yíng)銷、VI設(shè)計(jì)、 網(wǎng)站改版、漏洞修補(bǔ)等服務(wù)。為客戶提供更好的一站式互聯(lián)網(wǎng)解決方案,以客戶的口碑塑造優(yōu)易品牌,攜手廣大客戶,共同發(fā)展進(jìn)步。
(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="這是一句很長(zhǎng)的話|這是一句很長(zhǎng)的話|這是一句很長(zhǎng)的話|這是一句很長(zhǎng)的話|這是一句很長(zhǎng)的話|"
android:textAppearance="?android:attr/textAppearanceLarge" />顯示結(jié)果

可以看到這句話占了3行,并且是由系統(tǒng)自己判斷一行的剩余寬度能否再放下一個(gè)字符進(jìn)行換行的。
(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="這是一句很長(zhǎng)的話|這是一句很長(zhǎng)的話|這是一句很長(zhǎng)的話|這是一句很長(zhǎng)的話|這是一句很長(zhǎng)的話|"
android:textAppearance="?android:attr/textAppearanceLarge" />顯示結(jié)果

可以看到這時(shí)不管有多長(zhǎng)的一段話都會(huì)在一行內(nèi)顯示出來(lái),超過(guò)一行的部分使用...表示。
那若這時(shí)顯示的字?jǐn)?shù)不足一行時(shí)會(huì)怎么樣呢。
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="這是一句很長(zhǎng)的話|"
android:textAppearance="?android:attr/textAppearanceLarge" />顯示結(jié)果

這時(shí)并不會(huì)出現(xiàn)...的情況。
若想嚴(yán)格控制TextView的行數(shù)在一行之內(nèi)的可以使用這一屬性。
(3).使用lines屬性
實(shí)際行數(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="這是一句很長(zhǎng)的話|這是一句很長(zhǎng)的話|這是一句很長(zhǎng)的話|這是一句很長(zhǎng)的話|這是一句很長(zhǎng)的話|"
android:textAppearance="?android:attr/textAppearanceLarge" />顯示結(jié)果

這次加了一個(gè)lines的屬性,控制顯示的行數(shù)為2,可以看到,顯示的行數(shù)確實(shí)是2行,但是超出部分并不會(huì)如singLine那樣有...的提示,
并且這個(gè)屬性還有一個(gè)局限處,下面來(lái)看看,我們已經(jīng)知道正常顯示時(shí)是3行,可如果我們把lines的屬性值定為5會(huì)如何呢
實(shí)際行數(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="這是一句很長(zhǎng)的話|這是一句很長(zhǎng)的話|這是一句很長(zhǎng)的話|這是一句很長(zhǎng)的話|這是一句很長(zhǎng)的話|"
android:background="#888888"
android:textAppearance="?android:attr/textAppearanceLarge" />顯示結(jié)果

為了顯示明顯,這次在TextView上加上了背景色,可以看出來(lái),不管這些文本實(shí)際占了多少行,都會(huì)按照l(shuí)ines給出的數(shù)值去占位。
若不能確定這些文本究竟占了多少行,不要輕易使用lines屬性,若要使用,則這個(gè)值宜小不宜大。
(3).使用maxLines屬性
實(shí)際行數(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="這是一句很長(zhǎng)的話|這是一句很長(zhǎng)的話|這是一句很長(zhǎng)的話|這是一句很長(zhǎng)的話|這是一句很長(zhǎng)的話|"
android:background="#888888"
android:textAppearance="?android:attr/textAppearanceLarge" />顯示結(jié)果

設(shè)置了最大行數(shù)為5行,但是這個(gè)屬性會(huì)根據(jù)實(shí)際顯示進(jìn)行縮減,僅顯示實(shí)際內(nèi)容所占的行數(shù)。
若實(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="這是一句很長(zhǎng)的話|這是一句很長(zhǎng)的話|這是一句很長(zhǎng)的話|這是一句很長(zhǎng)的話|這是一句很長(zhǎng)的話|"
android:background="#888888"
android:textAppearance="?android:attr/textAppearanceLarge" />顯示結(jié)果

可以看到顯示結(jié)果很好的執(zhí)行了我們?cè)O(shè)置的最大行數(shù)屬性,只顯示出了兩行,雖然也沒(méi)有如singLine一樣給出...的還有內(nèi)容的提示。
個(gè)人認(rèn)為這是對(duì)于控制多行顯示中最好的方式。
(4).自主控制換行
若內(nèi)容自己知道,并且需要自行決定在什么位置換行,可以常用在文本中添加"\n"的方式進(jì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="這是一句很長(zhǎng)的話|\n這是一句很長(zhǎng)的話|\n這是一句很長(zhǎng)的話|\n這是一句很長(zhǎng)的話|\n這是一句很長(zhǎng)的話|"
android:background="#888888"
android:textAppearance="?android:attr/textAppearanceLarge" />顯示結(jié)果

這樣實(shí)現(xiàn)了自己決定何時(shí)換行。
綜上所述,
(1).對(duì)于文本內(nèi)容未知的情況,想要控制行數(shù)并可以最優(yōu)的顯示出文本內(nèi)容,當(dāng)采用maxLines屬性。
(2).對(duì)于文本內(nèi)容已知的情況,直接使用"\n"進(jìn)行手動(dòng)換行即可。
(3).以上的兩種情況,只想單行顯示時(shí),采用singLine屬性為最佳。
新聞標(biāo)題:TextView的行顯示策略
瀏覽地址:http://chinadenli.net/article22/gghjcc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、網(wǎng)站導(dǎo)航、微信公眾號(hào)、軟件開(kāi)發(fā)、關(guān)鍵詞優(yōu)化、標(biāo)簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)