Android layout屬性詳細說明
版本號 | 說明 | 作者 | 日期 |
1.0 | 添加Android4.2對應的View和TextView屬性的說明 | Sky Wang | 2013/06/15 |
|
文本總結了Android4.2 layout中用到的視圖屬性,包括View的屬性和TextView的屬性。這些屬性適用於所有繼承於View或者TextView的控件,包括Button、EditText、ListView、ImageView等等幾乎所有的widget控件。具體用到某個widget控件時,可以通過查看該widget的繼承關系,來判斷本文中的屬性是否適用於該控件。
例如,可以通過以下方法判斷Button是否能使用View或TextView的屬性。
打開Android官網Button頁面,查看有關Button的繼承關系。如下(紅色字體):
從上,我們可以看出Button繼承於TextView,所以TextView的所有屬性Button都可以使用;而TextView繼承於View,所以View的所有屬性Button也可以使用。
View屬性說明
下面對View的屬性進行詳細說明
android:alpha
關聯方法: setAlpha(float)
屬性說明: 視圖透明度,值在0-1之間。0為完全透明,1為完全不透明。
android:background
關聯方法: setBackgroundResource(int)
屬性說明: 視圖背景
android:clickable
關聯方法: setClickable(boolean)
屬性說明: 視圖是否可點擊
android:contentDescription
關聯方法: setContentDescription(CharSequence)
屬性說明: 設置View的備注說明,作為一種輔助功能提供,為一些沒有文字描述的View提供說明
android:drawingCacheQuality
關聯方法: setDrawingCacheQuality(int)
屬性說明: "設置繪圖時半透明質量。有可以取以下3個值 auto——默認,由框架決定 high——高質量,使用較高的顏色深度,消耗更多的內存 low——低質量,使用較低的顏色深度,但是用更少的內存"
android:duplicateParentState
關聯方法:
屬性說明: 如果設置此屬性,將直接從父容器中獲取繪圖狀態(光標,按下等)
android:fadeScrollbars
關聯方法: setScrollbarFadingEnabled(boolean)
屬性說明: 定義在ScrollBar沒有使用時,是否褪色。
android:fadingEdgeLength
關聯方法: getVerticalFadingEdgeLength()
屬性說明: 設置邊框漸變的長度。
android:filterTouchesWhenObscured
關聯方法: setFilterTouchesWhenObscured(boolean)
屬性說明: view所在窗口被其它可見窗口遮住時,是否過濾觸摸事件。
android:fitsSystemWindows
關聯方法: setFitsSystemWindows(boolean)
屬性說明: 設置布局調整時是否考慮系統窗口(如狀態欄)
android:focusable
關聯方法: setFocusable(boolean)
屬性說明: 設置是否獲得焦點。若有requestFocus()被調用時,后者優先處理。注意在表單中想設置某一個如EditText獲取焦點,光設置這個是不行的,需要將這個EditText前面的focusable都設置為false才行。在Touch模式下獲取焦點需要設置focusableInTouchMode為true。
android:focusableInTouchMode
關聯方法: setFocusableInTouchMode(boolean)
屬性說明: 設置在Touch模式下View是否能取得焦點。
android:hapticFeedbackEnabled
關聯方法: setHapticFeedbackEnabled(boolean)
屬性說明: 是否啟用觸摸反饋,啟用后就是在點擊等操作時會有震動等反饋效果
android:id
關聯方法: setId(int)
屬性說明: 給當前View設置一個在當前layout.xml中的唯一編號,可以通過調用View.findViewById() 或Activity.findViewById()根據這個編號查找到對應的View。不同的layout.xml之間定義相同的id不會沖突。
android:importantForAccessibility
關聯方法: setImportantForAccessibility(int)
屬性說明: 設置可達性的重要性
android:isScrollContainer
關聯方法: setScrollContainer(boolean)
屬性說明: 設置當前View為滾動容器。這里沒有測試出效果來,ListView/ GridView/ ScrollView根本就不用設置這個屬性,而EdidText設置android:scrollbars也能出滾動條
android:keepScreenOn
關聯方法: setKeepScreenOn(boolean)
屬性說明: 視圖在可見的情況下是否保持喚醒狀態。
android:layerType
關聯方法: setLayerType(int,Paint)
屬性說明: "設置指定層的類型,可以取以下3個值: none——不指定 software——軟件層。 hardware——硬件層。使用硬件加速。"
android:layoutDirection
關聯方法: setLayoutDirection(int)
屬性說明: 定義布局圖紙的方向
android:longClickable
關聯方法: setLongClickable(boolean)
屬性說明: 是否響應長點擊事件
android:minHeight
關聯方法: setMinimumHeight(int)
屬性說明: 設置視圖最小高度
android:minWidth
關聯方法: setMinimumWidth(int)
屬性說明: 設置視圖最小寬度
android:nextFocusDown
關聯方法: setNextFocusDownId(int)
屬性說明: 向下移動焦點時,下一個獲取焦點的view的id
android:nextFocusForward
關聯方法: setNextFocusForwardId(int)
屬性說明: 下一個獲取焦點的view的id
android:nextFocusLeft
關聯方法: setNextFocusLeftId(int)
屬性說明: 向左移動焦點時,下一個獲取焦點的view的id
android:nextFocusRight
關聯方法: setNextFocusRightId(int)
屬性說明: 向右移動焦點時,下一個獲取焦點的view的id
android:nextFocusUp
關聯方法: setNextFocusUpId(int)
屬性說明: 向上移動焦點時,下一個獲取焦點的view的id
android:onClick
關聯方法:
屬性說明: 點擊時,要調用的方法的名稱。
android:padding
關聯方法: setPaddingRelative(int,int,int,int)
屬性說明: 設置上下左右的邊距
android:paddingBottom
關聯方法: setPaddingRelative(int,int,int,int)
屬性說明: 下邊距
android:paddingEnd
關聯方法: setPaddingRelative(int,int,int,int)
屬性說明: 與android:paddingRight相同
android:paddingLeft
關聯方法: setPadding(int,int,int,int)
屬性說明: 左邊距
android:paddingRight
關聯方法: setPadding(int,int,int,int)
屬性說明: 右邊距
android:paddingStart
關聯方法: setPaddingRelative(int,int,int,int)
屬性說明: android:paddingLeft相同
android:paddingTop
關聯方法: setPaddingRelative(int,int,int,int)
屬性說明: 上邊距
android:requiresFadingEdge
關聯方法: setVerticalFadingEdgeEnabled(boolean)
屬性說明: 定義滾動時邊緣是否褪色
android:rotation
關聯方法: setRotation(float)
屬性說明: 旋轉度數
android:rotationX
關聯方法: setRotationX(float)
屬性說明: 水平旋轉度數
android:rotationY
關聯方法: setRotationY(float)
屬性說明: 豎直旋轉度數
android:saveEnabled
關聯方法: setSaveEnabled(boolean)
屬性說明: 在配置改變等情況出現時是否保存view的狀態數據。如果你的view有id,那默認系統就會幫你保存。
android:scaleX
關聯方法: setScaleX(float)
屬性說明: 水平方向縮放比例
android:scaleY
關聯方法: setScaleY(float)
屬性說明: 豎直方向縮放比例
android:scrollX
關聯方法:
屬性說明: x方向的滾動偏移。即在水平方向滾動了多少距離
android:scrollY
關聯方法:
屬性說明: y方向的滾動偏移。即在豎直方向滾動了多少距離
android:scrollbarAlwaysDrawHorizontalTrack
關聯方法:
屬性說明: 是否總是繪制水平滾動條的滾動軌道
android:scrollbarAlwaysDrawVerticalTrack
關聯方法:
屬性說明: 是否總是繪制豎直滾動條的滾動軌道
android:scrollbarDefaultDelayBeforeFade
關聯方法: setScrollBarDefaultDelayBeforeFade(int)
屬性說明: 滾動條在n毫秒后開始淡出。
android:scrollbarFadeDuration
關聯方法: setScrollBarFadeDuration(int)
屬性說明: 滾動條用多長時間淡出完畢。
android:scrollbarSize
關聯方法: setScrollBarSize(int)
屬性說明: 設置滾動條的尺寸。垂直滾動條的寬度、水平滾動條的高度
android:scrollbarStyle
關聯方法: setScrollBarStyle(int)
屬性說明: "滾動條的風格。共4組值: insideOverlay——內貼圖 insideInset——內插圖 outsideOverlay——外貼圖 outsideInset——外插圖。 inside就是滾動條在繪制在padding以內;outside就是不需要繪制在padding內(即view的邊界處);Overlay是貼圖,就是直接覆蓋在內容的上方,這樣內容可能會顯示到滾動條下方去;Inset是插圖,就是會在對應padding上加上滾動條的寬度,以不讓內容顯示到滾動條下面去。"
android:scrollbarThumbHorizontal
關聯方法:
屬性說明: 水平滾動塊的drawable對象
android:scrollbarThumbVertical
關聯方法:
屬性說明: 豎直滾動塊的drawable對象
android:scrollbarTrackHorizontal
關聯方法:
屬性說明: 水平滾動條滾動軌道的drawable對象
android:scrollbarTrackVertical
關聯方法:
屬性說明: 豎直滾動條滾動軌道的drawable對象
android:scrollbars
關聯方法:
屬性說明: "設置可顯示的滾動條。有3個取值: none——不顯示滾動條 horizontal——顯示水平滾動條 vertical——顯示豎直滾動條"
android:soundEffectsEnabled
關聯方法: setSoundEffectsEnabled(boolean)
屬性說明: 點擊或觸摸該view時,是否需要有聲音效果
android:tag
關聯方法:
屬性說明: string標識。類似id,id是整數標識。
android:textAlignment
關聯方法: setTextAlignment(int)
屬性說明: 設置文本的顯示方式。
android:textDirection
關聯方法: setTextDirection(int)
屬性說明: 設置文本的顯示方向。
android:transformPivotX
關聯方法: setPivotX(float)
屬性說明: 水平方向偏轉量
android:transformPivotY
關聯方法: setPivotY(float)
屬性說明: 豎直方向偏轉量
android:translationX
關聯方法: setTranslationX(float)
屬性說明: 水平方向的移動距離
android:translationY
關聯方法: setTranslationY(float)
屬性說明: 豎直方向的移動距離
android:visibility
關聯方法: setVisibility(int)
屬性說明: "view的可見性。有3個取值: gone——不可見,同時不占用view的空間; invisible——不可見,但占用view的空間; visible——可見"
TextView屬性說明
下面對TextView的屬性進行說明
android:autoLink
關聯方法: setAutoLinkMask(int)
屬性說明: 設置是否“當文本為URL鏈接/email/電話號碼/map時,文本顯示為可點擊的鏈接”。可選值(none/web/email/phone/map/all)
android:autoText
關聯方法: setKeyListener(KeyListener)
屬性說明: 如果設置,將自動執行輸入值的拼寫糾正。此處無效果,在顯示輸入法並輸入的時候起作用。
android:bufferType
關聯方法: setText(CharSequence,TextView.BufferType)
屬性說明: 指定getText()方式取得的文本類別。選項editable 類似於StringBuilder可追加字符,也就是說getText后可調用append方法設置文本內容。
android:capitalize
關聯方法: setKeyListener(KeyListener)
屬性說明: 設置自動大寫屬性。比如設置為2,自動大寫單詞首字符;設置為1,自動大寫每句話的首字母等等。
android:cursorVisible
關聯方法: setCursorVisible(boolean)
屬性說明: 設定光標為顯示/隱藏,默認顯示。
android:digits
關聯方法: setKeyListener(KeyListener)
屬性說明: 設置允許輸入哪些字符。如“1234567890.+-*/%\n()”
android:drawableBottom
關聯方法: setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)
屬性說明: 在text的下方輸出一個drawable。如果指定一個顏色的話會把text的背景設為該顏色,並且同時和background使用時覆蓋后者。
android:drawableEnd
關聯方法: setCompoundDrawablesRelativeWithIntrinsicBounds(int,int,int,int)
屬性說明: 在文本結尾處顯示drawable對象。它的值可以是其它資源的引用,比如,"@[+][package:]type:name"或者"?[package:][type:]name";也可以是顏色值,如"#rgb", "#argb", "#rrggbb", or "#aarrggbb"。
android:drawableLeft
關聯方法: setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)
屬性說明: 在text的左邊輸出一個drawable。
android:drawablePadding
關聯方法: setCompoundDrawablePadding(int)
屬性說明: 設置text與drawable的間隔,與drawableLeft、drawableRight、drawableTop、drawableBottom一起使用,可設置為負數,單獨使用沒有效果。
android:drawableRight
關聯方法: setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)
屬性說明: 在text的右邊輸出一個drawable。
android:drawableStart
關聯方法: setCompoundDrawablesRelativeWithIntrinsicBounds(int,int,int,int)
屬性說明: 在文本開始處顯示drawable對象。它的值可以是其它資源的引用,比如,"@[+][package:]type:name"或者"?[package:][type:]name";也可以是顏色值,如"#rgb", "#argb", "#rrggbb", or "#aarrggbb"。
android:drawableTop
關聯方法: setCompoundDrawablesWithIntrinsicBounds(int,int,int,int)
屬性說明: 在text的正上方輸出一個drawable。
android:editable
關聯方法:
屬性說明: 設置是否可編輯。這里無效果,在EditView中才有效果。
android:editorExtras
關聯方法: setInputExtras(int)
屬性說明: 設置文本的額外的輸入數據。在EditView中才有效果。
android:ellipsize
關聯方法: setEllipsize(TextUtils.TruncateAt)
屬性說明: 設置當文字過長時,該控件該如何顯示。有如下值設置:”start”—–省略號顯示在開頭;”end”——省略號顯示在結尾;”middle”—-省略號顯示在中間;”marquee” ——以跑馬燈的方式顯示(動畫橫向移動)
android:ems
關聯方法: setEms(int)
屬性說明: 設置TextView的寬度為N個字符的寬度。
android:fontFamily
關聯方法: setTypeface(Typeface)
屬性說明: 文本的字形體系。
android:freezesText
關聯方法: setFreezesText(boolean)
屬性說明: 設置保存文本的內容以及光標的位置。
android:gravity
關聯方法: setGravity(int)
屬性說明: 設置文本位置,如設置成“center”,文本將居中顯示。
android:height
關聯方法: setHeight(int)
屬性說明: 設置文本區域的高度,支持度量單位:px(像素)/dp/sp/in/mm(毫米)
android:hint
關聯方法: setHint(int)
屬性說明: Text為空時顯示的文字提示信息,可通過textColorHint設置提示信息的顏色。
android:imeActionId
關聯方法: setImeActionLabel(CharSequence,int)
屬性說明: 設置IME動作ID。
android:imeActionLabel
關聯方法: setImeActionLabel(CharSequence,int)
屬性說明: 設置IME動作標簽。在EditView再做說明。
android:imeOptions
關聯方法: setImeOptions(int)
屬性說明: 附加功能,設置右下角IME動作與編輯框相關的動作,如actionDone右下角將顯示一個“完成”,而不設置默認是一個回車符號。
android:includeFontPadding
關聯方法: setIncludeFontPadding(boolean)
屬性說明: 設置文本是否包含頂部和底部額外空白,默認為true。
android:inputMethod
關聯方法: setKeyListener(KeyListener)
屬性說明: 為文本指定輸入法,需要完全限定名(完整的包名)。例如:com.google.android.inputmethod.pinyin,但是這里報錯找不到。
android:inputType
關聯方法: setRawInputType(int)
屬性說明: 設置文本的類型,用於幫助輸入法顯示合適的鍵盤類型。在EditView中再詳細說明,這里無效果。
android:lineSpacingExtra
關聯方法: setLineSpacing(float,float)
屬性說明: 設置行間距。
android:lineSpacingMultiplier
關聯方法: setLineSpacing(float,float)
屬性說明: 設置行間距的倍數。如”1.2”
android:lines
關聯方法: setLines(int)
屬性說明: 設置文本的行數,設置兩行就顯示兩行,即使第二行沒有數據。
android:linksClickable
關聯方法: setLinksClickable(boolean)
屬性說明: 設置鏈接是否點擊連接,即使設置了autoLink。
android:marqueeRepeatLimit
關聯方法: setMarqueeRepeatLimit(int)
屬性說明: 在ellipsize指定marquee的情況下,設置重復滾動的次數,當設置為marquee_forever時表示無限次。
android:maxEms
關聯方法: setMaxEms(int)
屬性說明: 設置TextView的寬度為最長為N個字符的寬度。與ems同時使用時覆蓋ems選項。
android:maxHeight
關聯方法: setMaxHeight(int)
屬性說明: 設置文本區域的最大高度
android:maxLength
關聯方法: setFilters(InputFilter)
屬性說明: 限制顯示的文本長度,超出部分不顯示。
android:maxLines
關聯方法: setMaxLines(int)
屬性說明: 設置文本的最大顯示行數,與width或者layout_width結合使用,超出部分自動換行,超出行數將不顯示。
android:maxWidth
關聯方法: setMaxWidth(int)
屬性說明: 設置文本區域的最大寬度
android:minEms
關聯方法: setMinEms(int)
屬性說明: 設置TextView的寬度為最短為N個字符的寬度。與ems同時使用時覆蓋ems選項。
android:minHeight
關聯方法: setMinHeight(int)
屬性說明: 設置文本區域的最小高度
android:minLines
關聯方法: setMinLines(int)
屬性說明: 設置文本的最小行數,與lines類似。
android:minWidth
關聯方法: setMinWidth(int)
屬性說明: 設置文本區域的最小寬度
android:numeric
關聯方法: setKeyListener(KeyListener)
屬性說明: 如果被設置,該TextView有一個數字輸入法。此處無用,設置后唯一效果是TextView有點擊效果,此屬性在EdtiView將詳細說明。
android:password
關聯方法: setTransformationMethod(TransformationMethod)
屬性說明: 以小點”.”顯示文本
android:phoneNumber
關聯方法: setKeyListener(KeyListener)
屬性說明: 設置為電話號碼的輸入方式。
android:privateImeOptions
關聯方法: setPrivateImeOptions(String)
屬性說明: 設置輸入法選項,在EditText中才有作用。
android:scrollHorizontally
關聯方法: setHorizontallyScrolling(boolean)
屬性說明: 設置文本超出TextView的寬度的情況下,是否出現橫拉條。
android:selectAllOnFocus
關聯方法: setSelectAllOnFocus(boolean)
屬性說明: 如果文本是可選擇的,讓他獲取焦點而不是將光標移動為文本的開始位置或者末尾位置。TextView中設置后無效果。
android:shadowColor
關聯方法: setShadowLayer(float,float,float,int)
屬性說明: 指定文本陰影的顏色,需要與shadowRadius一起使用。
android:shadowDx
關聯方法: setShadowLayer(float,float,float,int)
屬性說明: 設置陰影橫向坐標開始位置。
android:shadowDy
關聯方法: setShadowLayer(float,float,float,int)
屬性說明: 設置陰影縱向坐標開始位置。
android:shadowRadius
關聯方法: setShadowLayer(float,float,float,int)
屬性說明: 設置陰影的半徑。設置為0.1就變成字體的顏色了,一般設置為3.0的效果比較好。
android:singleLine
關聯方法: setTransformationMethod(TransformationMethod)
屬性說明: 設置單行顯示。如果和layout_width一起使用,當文本不能全部顯示時,后面用“…”來表示。如android:text="test_ singleLine " android:singleLine="true" android:layout_width="20dp"將只顯示“t…”。如果不設置singleLine或者設置為false,文本將自動換行
android:text
關聯方法: setText(CharSequence,TextView.BufferType)
屬性說明: 設置顯示文本.
android:textAllCaps
關聯方法: setAllCaps(boolean)
屬性說明: 設置文本全為大寫。值為"true"或"false"。
android:textAppearance
關聯方法:
屬性說明: 設置文字外觀。如“?android:attr/textAppearanceLargeInverse
android:textColor
關聯方法: setTextColor(int)
屬性說明: 設置文本顏色
android:textColorHighlight
關聯方法: setHighlightColor(int)
屬性說明: 被選中文字的底色,默認為藍色
android:textColorHint
關聯方法: setHintTextColor(int)
屬性說明: 設置提示信息文字的顏色,默認為灰色。與hint一起使用。
android:textColorLink
關聯方法: setLinkTextColor(int)
屬性說明: 文字鏈接的顏色.
android:textIsSelectable
關聯方法: isTextSelectable()
屬性說明: 設置非編輯文本可否被選擇。值為"true"或"false"。
android:textScaleX
關聯方法: setTextScaleX(float)
屬性說明: 設置文字之間間隔,默認為1.0f。
android:textSize
關聯方法: setTextSize(int,float)
屬性說明: 設置文字大小,推薦度量單位”sp”,如”15sp”
android:textStyle
關聯方法: setTypeface(Typeface)
屬性說明: 設置字形[bold(粗體) 0, italic(斜體) 1, bolditalic(又粗又斜) 2] 可以設置一個或多個,用“|”隔開
android:typeface
關聯方法: setTypeface(Typeface)
屬性說明: 設置文本字體,必須是以下常量值之一:normal 0, sans 1, serif 2, monospace(等寬字體) 3]
android:width
關聯方法: setWidth(int)
屬性說明: 設置文本區域的寬度,支持度量單位:px(像素)/dp/sp/in/mm(毫米)。
參考文檔
1, (View)Android 官方文檔 —— View
2, (TextView)Android 官方文檔 —— TextView
3, (View)Android2.2 API 中文文檔系列(5) —— View
4, (View)view使用
5, (View)android ListView幾個比較特別的屬性
6, (View)第三講 Layout文件屬性講解
7, (TextView)Android2.2 API 中文文檔系列(1) —— TextView
更多內容參考“skywang的博客目錄”