Displays text to the user and optionally allows them to edit it. A TextView is a complete text editor, however the basic class is configured to not allow editing; see for a subclass that configures the text view for editing.
這是Google官方對TextView的一個介紹:向用戶顯示文本式廷,并允許用戶編輯文本。TextView是一個完整的文本編輯器滥酥,但是基本類配置為不允許編輯;查看配置用于編輯的文本視圖的子類捐康。
常用屬性
- android:autoLink設(shè)置是否當(dāng)文本為URL鏈接/email/電話號碼/map時,文本顯示為可點擊的鏈接入热∨淖兀可選值(none/web/email/phone/map/all)
- android:autoText如果設(shè)置,將自動執(zhí)行輸入值的拼寫糾正勺良。此處無效果绰播,在顯示輸入法并輸入的時候起作用。
- android:bufferType指定getText()方式取得的文本類別尚困。選項editable 類似于StringBuilder可追加字符蠢箩,
也就是說getText后可調(diào)用append方法設(shè)置文本內(nèi)容。 - android:capitalize設(shè)置英文字母大寫類型事甜。此處無效果谬泌,需要彈出輸入法才能看得到,參見EditView此屬性說明逻谦。
- android:cursorVisible設(shè)定光標(biāo)為顯示/隱藏掌实,默認(rèn)顯示。
- android:digits設(shè)置允許輸入哪些字符邦马。如“1234567890.+-*/% ()”
- android:editable設(shè)置是否可編輯贱鼻。
- android:editorExtras設(shè)置文本的額外的輸入數(shù)據(jù)宴卖。
- android:ellipsize設(shè)置當(dāng)文字過長時,該控件該如何顯示。有如下值設(shè)置:”start”—?省略號顯示在開頭;”end”——省略號顯示在結(jié)尾;”middle”—-省略號顯示在中間; ”marquee” ——以跑馬燈的方式顯示(動畫橫向移動)
- android:freezesText設(shè)置保存文本的內(nèi)容以及光標(biāo)的位置邻悬。
- android:imeOptions附加功能嘱腥,設(shè)置右下角IME動作與編輯框相關(guān)的動作,如actionDone右下角將顯示一個“完成”拘悦,而不設(shè)置默認(rèn)是一個回車符號齿兔。這個在EditView中再詳細(xì) 說明,此處無用础米。
- android:imeActionId設(shè)置IME動作ID分苇。
- android:imeActionLabel設(shè)置IME動作標(biāo)簽。
- android:includeFontPadding設(shè)置文本是否包含頂部和底部額外空白屁桑,默認(rèn)為true医寿。
- android:inputMethod為文本指定輸入法,需要完全限定名(完整的包名)蘑斧。例如:com.google.android.inputmethod.pinyin靖秩,但是這里報錯找不到。
- android:inputType設(shè)置文本的類型竖瘾,用于幫助輸入法顯示合適的鍵盤類型沟突。在EditView中再詳細(xì)說明,這里無效果捕传。
- android:linksClickable設(shè)置鏈接是否點擊連接惠拭,即使設(shè)置了autoLink。
- android:marqueeRepeatLimit在ellipsize指定marquee的情況下庸论,設(shè)置重復(fù)滾動的次數(shù)职辅,當(dāng)設(shè)置為marquee_forever時表示無限次。
- android:ems設(shè)置TextView的寬度為N個字符的寬度聂示。這里測試為一個漢字字符寬度
- android:maxEms設(shè)置TextView的寬度為最長為N個字符的寬度域携。與ems同時使用時覆蓋ems選項。
- android:minEms設(shè)置TextView的寬度為最短為N個字符的寬度鱼喉。與ems同時使用時覆蓋ems選項秀鞭。
- android:maxLength限制顯示的文本長度,超出部分不顯示蒲凶。
- android:lines設(shè)置文本的行數(shù)气筋,設(shè)置兩行就顯示兩行拆内,即使第二行沒有數(shù)據(jù)旋圆。
- android:maxLines設(shè)置文本的最大顯示行數(shù),與width或者layout_width結(jié)合使用麸恍,超出部分自動換行灵巧,超出行數(shù)將不顯示搀矫。
- android:minLines設(shè)置文本的最小行數(shù),與lines類似刻肄。
- android:lineSpacingExtra設(shè)置行間距瓤球。
- android:lineSpacingMultiplier設(shè)置行間距的倍數(shù)。如”1.2”
- android:numeric如果被設(shè)置敏弃,該TextView有一個數(shù)字輸入法卦羡。此處無用,設(shè)置后唯一效果是TextView有點擊效果麦到,此屬性在EdtiView將詳細(xì)說明绿饵。
- android:password以小點”.”顯示文本
- android:phoneNumber設(shè)置為電話號碼的輸入方式。
- android:privateImeOptions設(shè)置輸入法選項瓶颠,此處無用拟赊,在EditText將進(jìn)一步討論。
- android:scrollHorizontally設(shè)置文本超出TextView的寬度的情況下粹淋,是否出現(xiàn)橫拉條吸祟。
- android:selectAllOnFocus如果文本是可選擇的,讓他獲取焦點而不是將光標(biāo)移動為文本的開始位置或者末尾位置桃移。TextView中設(shè)置后無效果屋匕。
- android:shadowColor指定文本陰影的顏色,需要與shadowRadius一起使用借杰。
- android:shadowDx設(shè)置陰影橫向坐標(biāo)開始位置炒瘟。
- android:shadowDy設(shè)置陰影縱向坐標(biāo)開始位置。
- android:shadowRadius設(shè)置陰影的半徑第步。設(shè)置為0.1就變成字體的顏色了疮装,一般設(shè)置為3.0的效果比較好。
- android:singleLine設(shè)置單行顯示粘都。如果和layout_width一起使用廓推,當(dāng)文本不能全部顯示時,后面用“…”來表示翩隧。如android:text="test_ singleLine " 樊展,android:singleLine="true" android:layout_width="20dp"將只顯示“t…”。如果不設(shè)置singleLine或者設(shè)置為false堆生,文本將自動換行
- android:textAppearance設(shè)置文字外觀专缠。如“?android:attr/textAppearanceLargeInverse”這里引用的是系統(tǒng)自帶的一個外觀,?表示系統(tǒng)是否有這種外觀淑仆,否則使用默認(rèn)的外觀涝婉。可設(shè)置的值如下: textAppearanceButton/textAppearanceInverse/textAppearanceLarge/textAppearanceLargeInverse/textAppearanceMedium/textAppearanceMediumInverse/textAppearanceSmall/textAppearanceSmallInverse
- android:textColorHighlight被選中文字的底色蔗怠,默認(rèn)為藍(lán)色
- android:textColorLink文字鏈接的顏色.
- android:textScaleX設(shè)置文字之間間隔墩弯,默認(rèn)為1.0f吩跋。
- android:textStyle設(shè)置字形[bold(粗體) 0, italic(斜體) 1, bolditalic(又粗又斜) 2] 可以設(shè)置一個或多個,用“|”隔開
- android:typeface設(shè)置文本字體渔工,必須是以下常量值之一:normal 0, sans 1, serif 2, monospace(等寬字體) 3
這里初略的列舉出了TextView的屬性值锌钮,當(dāng)然那些常識性的屬性就沒有列舉了,還有一些屬性即使設(shè)置了也沒有效果引矩,只有在TextView的子類EditText里面才會起作用梁丘,像輸入法以及軟鍵盤相關(guān)的一些屬性,在TextView上面設(shè)置都是沒有用的旺韭±家鳎基本上常用的一些功能,都可以在xml中直接配置并實現(xiàn)茂翔,像跑馬燈滾動混蔼,文本左右的圖片等。
一個滑動小功能
下面介紹一個TextView文本上下滑動功能珊燎,可能有的人會說了惭嚣,其實可以直接使用EditText,設(shè)置成不可編輯就可以了悔政,這也是一種解決辦法晚吞,我們這里用TextView實現(xiàn)。只需要設(shè)置一個屬性就可以了(當(dāng)然前提是你的TextView是固定高度的) 谋国,這個屬性只能通過Java語句設(shè)置槽地,不能在xml文件里面設(shè)置,
textView.setMovementMethod(ScrollingMovementMethod.getInstance());
當(dāng)然也可以在xml里面去定義滑塊的顯示和樣式芦瘾。
TextView的常用子類
Button
A user interface element the user can tap or click to perform an action.
Button去Api里面看源碼捌蚊,就會發(fā)現(xiàn)他非常的簡單,就只是在繼承TextView的同時近弟,在構(gòu)造方法中給與了Button特有的style缅糟,
public Button(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.buttonStyle);
}
沒有什么額外的操作,也就在8.0以后加入了onResolvePointerIcon方法祷愉,手指點擊后的一個動態(tài)效果窗宦。
CheckBox
A checkbox is a specific type of two-states button that can be either checked or unchecked.
CheckBox和Button基本上是一樣的,都是在構(gòu)造方法中給了一個CheckBox特有的style二鳄,別的也沒有什么特殊的操作赴涵。
public CheckBox(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.checkboxStyle);
}
EditText
This is supposed to be a very thin veneer over TextView. Do not make any changes here that do anything that a TextView with a key listener and a movement method wouldn't do!
EditText也和Button,CheckBox一樣订讼,在構(gòu)造方法中給與了EditText特有的style髓窜,
public EditText(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.editTextStyle);
}
所不同的是,EditText里面還自動集成了復(fù)制躯嫉,全選復(fù)制的功能纱烘,并將文本采集到粘貼板中。
/**
* Convenience for {@link Selection#setSelection(Spannable, int, int)}.
*/
public void setSelection(int start, int stop) {
Selection.setSelection(getText(), start, stop);
}
/**
* Convenience for {@link Selection#setSelection(Spannable, int)}.
*/
public void setSelection(int index) {
Selection.setSelection(getText(), index);
}
/**
* Convenience for {@link Selection#selectAll}.
*/
public void selectAll() {
Selection.selectAll(getText());
}
/**
* Convenience for {@link Selection#extendSelection}.
*/
public void extendSelection(int index) {
Selection.extendSelection(getText(), index);
}
當(dāng)然EditText還有其他的功能祈餐,但是這些功能都可以最開始提到的TextView的屬性值一一實現(xiàn)擂啥。
TextView肯定還有其他的子類,但是我們通常使用的也就只有這三種帆阳,其他的就不做介紹哺壶。