Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 中階開發

Android 開發:TextView 屬性完整中文對照

編輯:中階開發

TextView 是 Android 裡最基礎也最常被使用的控制項,但它的屬性多到令人卻步。以下依功能分門別類整理,方便查閱。要留意的是,部分屬性在 TextView 上實際不會生效,需要搭配 EditText 並彈出輸入法後才看得到效果,文中會特別標註。

一、連結與自動辨識

android:autoLink 當文字內容為 URL、email、電話號碼或地圖資訊時,是否讓它變成可點擊的連結。可選值:none / web / email / phone / map / all。

android:linksClickable 即使設定了 autoLink,仍可透過這個屬性決定連結能不能被點擊。

android:textColorLink 設定文字中連結的顏色。

android:autoText 設定後會自動對輸入內容做拼字校正。此處無效果,需在彈出輸入法並輸入時才起作用。

二、文字內容與緩衝類型

android:text 設定要顯示的文字。

android:hint 文字為空時顯示的提示訊息,可搭配 textColorHint 調整顏色。主要用於 EditText,但在 TextView 上也可用。

android:textColorHint 提示文字的顏色,預設為灰色,需與 hint 一起使用。

android:bufferType 指定 getText() 取回的文字類型。editable 類似 StringBuilder,可呼叫 append 追加內容;spannable 則可在指定字元區間套用樣式。

android:freezesText 設定是否保存文字內容與游標位置。

android:cursorVisible 游標顯示或隱藏,預設顯示。

android:editable 設定是否可編輯。

android:digits 設定允許輸入哪些字元,例如 "1234567890.+-*/% ()"。

android:maxLength 限制顯示的文字長度,超出部分不顯示。

android:passWord 以小圓點「.」顯示文字。

android:phoneNumber 設定為電話號碼的輸入方式。

android:numeric 設定後該 TextView 會啟用數字輸入法。此處唯一可見的效果是 TextView 變得可點擊,實際行為要在 EditText 中才完整。

android:capitalize 設定英文字母大寫類型。此處無效果,需彈出輸入法才能觀察。

三、輸入法相關(多數需搭配 EditText)

android:inputType 設定文字類型,用以幫助輸入法顯示合適的鍵盤配置。

android:inputMethod 為文字指定輸入法,需使用完整套件名稱(例如 com.google.android.inputmethod.pinyin)。

android:imeOptions 設定右下角 IME 動作,例如 actionDone 會顯示「完成」而非預設的換行符號。

android:imeActionId 設定 IME 動作的 ID。

android:imeActionLabel 設定 IME 動作的標籤。

android:privateImeOptions 設定輸入法的額外選項。

android:editorExtras 設定文字的額外輸入資料。

四、Drawable 圖示與間距

android:drawableLeft 在文字左側輸出一個 drawable。

android:drawableRight 在文字右側輸出一個 drawable。

android:drawableTop 在文字上方輸出一個 drawable。

android:drawableBottom 在文字下方輸出一個 drawable。若指定的是顏色,會把文字背景設為該色,並覆蓋 background 的設定。

android:drawablePadding 設定文字與 drawable 之間的間隔。需與上述四個方向屬性一起使用,可設為負數;單獨使用沒有效果。

五、文字外觀

android:textAppearance 設定文字外觀,例如 ?android:attr/textAppearanceLargeInverse。問號表示先詢問系統是否有此樣式,若無則使用預設。可用值:textAppearanceButton、textAppearanceInverse、textAppearanceLarge、textAppearanceLargeInverse、textAppearanceMedium、textAppearanceMediumInverse、textAppearanceSmall、textAppearanceSmallInverse。

android:textColor 文字顏色。

android:textColorHighlight 被選取文字的底色,預設為藍色。

android:textScaleX 文字之間的間隔倍率,預設 1.0f。

android:textSize 文字大小,建議使用 sp 單位,例如 15sp。

android:textStyle 字形樣式:bold(粗體,0)、italic(斜體,1)、bolditalic(粗斜體,2)。可組合設定,以 | 分隔。

android:typeface 文字字型,必須是 normal(0)、sans(1)、serif(2)、monospace(3,等寬字型) 之一。

android:includeFontPadding 文字是否包含頂部與底部的額外空白,預設為 true。

六、陰影效果

android:shadowColor 文字陰影的顏色,需與 shadowRadius 一起使用。

android:shadowDx 陰影的橫向起始位移。

android:shadowDy 陰影的縱向起始位移。

android:shadowRadius 陰影的擴散半徑。設為 0.1 時會幾乎變成字體顏色本身,一般設為 3.0 左右效果較自然。

七、寬度與行數控制

android:ems 把 TextView 寬度設為 N 個字元寬(實測約為一個全形字寬)。

android:maxEms 寬度上限為 N 個字元,與 ems 同時使用時會覆蓋 ems。

android:minEms 寬度下限為 N 個字元,與 ems 同時使用時會覆蓋 ems。

android:lines 固定顯示的行數。設為兩行就佔兩行空間,即使第二行沒有內容。

android:maxLines 最大顯示行數,需與 width 或 layout_width 搭配;超出部分自動換行,超過行數則不顯示。

android:minLines 最小行數,概念與 lines 類似。

android:singleLine 強制單行顯示。若與 layout_width 一起使用,文字無法完整顯示時結尾會以「…」表示。例如寬度設為 20dp 時只會顯示「t…」;若不設定或設為 false,文字會自動換行。

android:scrollHorizontally 文字超出 TextView 寬度時,是否出現水平捲軸。

八、對齊與間距

android:gravity 文字在控制項內的位置,例如設為 center 即置中顯示。

android:lineSpacingExtra 額外的行間距。

android:lineSpacingMultiplier 行間距倍數,例如 1.2。

android:height 文字區域高度,支援 px / dp / sp / in / mm。

android:maxHeight 文字區域最大高度。

android:minHeight 文字區域最小高度。

android:width 文字區域寬度,支援 px / dp / sp / in / mm。

android:maxWidth 文字區域最大寬度。

android:minWidth 文字區域最小寬度。

九、文字過長時的處理

android:ellipsize 當文字過長時的顯示策略:

· start — 省略號顯示在開頭
· end — 省略號顯示在結尾
· middle — 省略號顯示在中間
· marquee — 以跑馬燈方式橫向捲動顯示

android:marqueeRepeatLimit 在 ellipsize 設為 marquee 時,設定重複捲動的次數;設為 marquee_forever 表示無限次。

十、焦點與選取

android:selectAllOnFocus 若文字可選取,取得焦點時直接全選,而非把游標移到開頭或結尾。在 TextView 上設定後無效果。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved