一、TextView控件的常用屬性
1、android:id——控件的id
2、android:layout_width——設置控件的寬度
wrap_content(包裹實際文本內容)
fill_parent(當前控件鋪滿父類容器)
match_parent(當前控件鋪滿父類容器,2.3api之后添加一個屬性值)
支持度量單位:px(像素)/dp/sp/in/mm(毫米)
3、android:maxWidth——設置控件的最大寬度
wrap_content(包裹實際文本內容)
fill_parent(當前控件鋪滿父類容器)
match_parent(當前控件鋪滿父類容器,2.3api之后添加一個屬性值)
支持度量單位:px(像素)/dp/sp/in/mm(毫米)
4、android:minWidth——設置控件的最小寬度
wrap_content(包裹實際文本內容)
fill_parent(當前控件鋪滿父類容器)
match_parent(當前控件鋪滿父類容器,2.3api之后添加一個屬性值)
支持度量單位:px(像素)/dp/sp/in/mm(毫米)
5、android:layout_height——設置控件的高度
wrap_content(包裹實際文本內容)
fill_parent(當前控件鋪滿父類容器)
match_parent(當前控件鋪滿父類容器,2.3api之后添加一個屬性值)
支持度量單位:px(像素)/dp/sp/in/mm(毫米)
6、android:maxHeight——設置控件的最大高度
wrap_content(包裹實際文本內容)
fill_parent(當前控件鋪滿父類容器)
match_parent(當前控件鋪滿父類容器,2.3api之后添加一個屬性值)
支持度量單位:px(像素)/dp/sp/in/mm(毫米)
7、android:minHeight——設置控件的最小高度
wrap_content(包裹實際文本內容)
fill_parent(當前控件鋪滿父類容器)
match_parent(當前控件鋪滿父類容器,2.3api之后添加一個屬性值)
支持度量單位:px(像素)/dp/sp/in/mm(毫米)
8、android:background——設置控件的背景顏色
可以使用十六進制(可以直接在左邊選擇顏色),也可以在資源文件里設置好顏色值后通過id的形式引用
9、android:textColor——設置文字顏色
可以使用十六進制(可以直接在左邊選擇顏色),也可以在資源文件里設置好顏色值后通過id的形式引用
10、android:text——文本內容
設置文本內容的時候可以直接寫文字,但最好在string.xml中設置好文字后通過id引用
11、android:textSize——設置文字大小
推薦使用單位"sp"
12、android:textStyle——字體樣式
[bold(粗體) 0, italic(斜體) 1, bolditalic(又粗又斜) 2] 可以設置一個或多個,用“|”隔開
13、android:typeface——設置文本字體
必須是以下常量值之一:normal 0, sans 1, serif 2, monospace(等寬字體) 3]
14、android:textScaleX——設置文字之間間隔
默認為1.0f
15、android:lines——設置文本的行數
設置兩行就顯示兩行,即使第二行沒有數據。
16、android:singleLine——是否單行顯示
如果和layout_width一起使用,當文本不能全部顯示時,后面用“…”來表示。
android:singleLine="true" android:layout_width="20dp"將只顯示“t…”。如果不設置singleLine或者設置為false,文本將自動換行
17、android:maxLines——設置文本的最大顯示行數
與width或者layout_width結合使用,超出部分自動換行,超出行數將不顯示。
18、android:minLines——設置文本的最小行數
與lines類似。
19、android:autoLink:設置是否當文本為URL鏈接/email/電話號碼/map時,文本顯示為可點擊的鏈接。
可選值(none/web/email/phone/map/all)
20、android:textColorLink——文字鏈接的顏色.
可以使用十六進制(可以直接在左邊選擇顏色),也可以在資源文件里設置好顏色值后通過id的形式引用
21、android:gravity——設置文字對齊方式
如設置成“center”,文本將居中顯示。
22、android:textIsSelectable——允許用戶做出選擇的手勢,進而觸發系統內置的復制/粘貼控制
值可以為"true/false"
23、android:drawableRight在text的右邊輸出一個drawable。
24、android:drawableTop在text的正上方輸出一個drawable。
25、android:drawableBottom在text的下方輸出一個drawable
26、android:drawableLeft在text的左邊輸出一個drawable
27、android:drawablePadding設置text與drawable(圖片)的間隔
與drawableLeft、drawableRight、drawableTop、drawableBottom一起使用,可設置為負數,單獨使用沒有效果。
二、代碼示例
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="New Text" android:id="@+id/textView" android:textIsSelectable="true" android:background="#ff37af" android:textSize="25sp" android:textColor="#0942ff" android:textScaleX="2.0f" android:gravity="right" android:lines="2" android:autoLink="email" android:textColorLink="#03ff22" android:textColorHighlight="#02e9ff" android:textStyle="italic|bold"/> </LinearLayout>