【移動應(yīng)用開發(fā)技術(shù)】TextView的行顯示策略_第1頁
【移動應(yīng)用開發(fā)技術(shù)】TextView的行顯示策略_第2頁
【移動應(yīng)用開發(fā)技術(shù)】TextView的行顯示策略_第3頁
【移動應(yīng)用開發(fā)技術(shù)】TextView的行顯示策略_第4頁
【移動應(yīng)用開發(fā)技術(shù)】TextView的行顯示策略_第5頁
免費預(yù)覽已結(jié)束,剩余1頁可下載查看

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

【移動應(yīng)用開發(fā)技術(shù)】TextView的行顯示策略

在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屬性值2xml代碼<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屬性值5xml代碼<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ù)5xml代碼<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"

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論