TextView的顯示方式

在Android項目中常常會有需要TextView顯示很長文本的時候,這時候TextView的換行策略就成了一大問題,但是如何使TextView執行自己想要的換行效果呢。TextView的屬性中有幾點是可以起到作用的。

(1).正常顯示

xml代碼

1
2
3
4
5
6
7
8
<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"/>

顯示結果

wKioL1NCN8GDVfEgAAD_YaAa-6U252.jpg

可以看到這句話佔了3行,並且是由系統自己判斷一行的剩餘寬度能否再放下一個字符進行換行的。

(2).使用singleLine屬性

xml代碼

1
2
3
4
5
6
7
8
9
<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"/>

顯示結果

wKiom1NCOPPQkejjAACkhUoUmiI869.jpg

可以看到這時不管有多長的一段話都會在一行內顯示出來,超過一行的部分使用...表示。

那若這時顯示的字數不足一行時會怎麼樣呢。

xml代碼

1
2
3
4
5
6
7
8
9
<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"/>

顯示結果

wKioL1NCOXGyL9bxAABzBXDoYn4020.jpg

這時並不會出現...的情況。

若想嚴格控制TextView的行數在一行之內的可以使用這一屬性。

(3).使用lines屬性

實際行數3>lines屬性值2

xml代碼

1
2
3
4
5
6
7
8
9
<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"/>

顯示結果

wKioL1NCOk2SXJhgAADJUBT6fK4167.jpg

這次加了一個lines的屬性,控制顯示的行數爲2,可以看到,顯示的行數確實是2行,但是超出部分並不會如singLine那樣有...的提示,

並且這個屬性還有一個侷限處,下面來看看,我們已經知道正常顯示時是3行,可如果我們把lines的屬性值定爲5會如何呢

實際行數3<lines屬性值5

xml代碼

1
2
3
4
5
6
7
8
9
10
<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"/>

顯示結果

wKioL1NCOz_h0U2RAADkUNU3y-c795.jpg

爲了顯示明顯,這次在TextView上加上了背景色,可以看出來,不管這些文本實際佔了多少行,都會按照lines給出的數值去佔位。

若不能確定這些文本究竟佔了多少行,不要輕易使用lines屬性,若要使用,則這個值宜小不宜大。

(3).使用maxLines屬性

實際行數3<最大行數5

xml代碼

1
2
3
4
5
6
7
8
9
10
<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"/>

顯示結果

wKiom1NCPNCSo_AxAADXhbBl18Q491.jpg

設置了最大行數爲5行,但是這個屬性會根據實際顯示進行縮減,僅顯示實際內容所佔的行數。

實際行數3>最大行數2的情況呢?

xml代碼

1
2
3
4
5
6
7
8
9
10
<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"/>

顯示結果

wKioL1NCPZPjfuG6AAClil_bW3E021.jpg

可以看到顯示結果很好的執行了我們設置的最大行數屬性,只顯示出了兩行,雖然也沒有如singLine一樣給出...的還有內容的提示。

個人認爲這是對於控制多行顯示中最好的方式。

(4).自主控制換行

若內容自己知道,並且需要自行決定在什麼位置換行,可以常用在文本中添加"\n"的方式進行換行

xml代碼

1
2
3
4
5
6
7
8
9
<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"/>

顯示結果

wKioL1NCPvOzUDqkAAC7GL2UK-Q411.jpg

這樣實現了自己決定何時換行。


綜上所述,

(1).對於文本內容未知的情況,想要控制行數並可以最優的顯示出文本內容,當採用maxLines屬性。

(2).對於文本內容已知的情況,直接使用"\n"進行手動換行即可。

(3).以上的兩種情況,只想單行顯示時,採用singLine屬性爲最佳。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章