Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

我的 Android 之旅(二十):常用控件屬性速查大全

編輯:關於 Android 程式設計

學 Android 一個多月,天天寫代碼、天天用控件,常用屬性大致能記住了。可越往深學越發現,不少看似不起眼的屬性,真要用的時候卻一知半解,最後只能靠搜索救急。趁這個機會把常用屬性系統整理一遍,之後翻閱查漏,也是不錯的積累。

一、布爾型屬性(true / false)

這組主要用於 RelativeLayout 的對齊與定位:

  • android:layout_centerHorizontal:水平居中
  • android:layout_centerVertical:垂直居中
  • android:layout_centerInParent:相對於父元素完全居中
  • android:layout_alignParentBottom:貼緊父元素下邊緣
  • android:layout_alignParentLeft:貼緊父元素左邊緣
  • android:layout_alignParentRight:貼緊父元素右邊緣
  • android:layout_alignParentTop:貼緊父元素上邊緣
  • android:layout_alignWithParentIfMissing:對應兄弟元素找不到時,以父元素作參照

二、id 引用型屬性(@id/id-name)

這組用於相對於某個 id 控件定位:

  • android:layout_below:位於某元素下方
  • android:layout_above:位於某元素上方
  • android:layout_toLeftOf:位於某元素左邊
  • android:layout_toRightOf:位於某元素右邊
  • android:layout_alignTop:本元素上邊緣與某元素上邊緣對齊
  • android:layout_alignLeft:本元素左邊緣與某元素左邊緣對齊
  • android:layout_alignBottom:本元素下邊緣與某元素下邊緣對齊
  • android:layout_alignRight:本元素右邊緣與某元素右邊緣對齊
  • android:layout_alignBaseline:與某元素的 baseline 對齊

三、具體尺寸型屬性(如 30dip、40px)

  • android:layout_marginBottom:距某元素底邊緣的距離
  • android:layout_marginLeft:距某元素左邊緣的距離
  • android:layout_marginRight:距某元素右邊緣的距離
  • android:layout_marginTop:距某元素上邊緣的距離

四、常用通用屬性

  • android:id:為控件指定 ID
  • android:text:控件顯示的文字,建議引用 strings.xml
  • android:hint:EditText 為空時的提示信息
  • android:textSize:字體大小
  • android:background:背景色(RGB 命名法),設透明可用 @android:color/transparent
  • android:width / android:height:控件自身寬高
  • android:layout_width / android:layout_height:容器佈局中的寬高,常用 fill_parent(填滿父容器)與 wrap_content(剛好容納內容)
  • android:layout_weight:按比例劃分空間,編排等分佈局時非常關鍵
  • android:padding*:控件內容到邊緣的內邊距
  • android:singleLine:設為 true 時內容單行顯示
  • android:orientation:LinearLayout 方向,vertical 垂直、horizontal 水平

五、gravity 與 layout_gravity 之別

這兩個最容易搞混:

  • android:gravity:控制「控件內容」在其自身範圍內的位置,比如按鈕上的文字靠左、靠右、居中。
  • android:layout_gravity:控制「控件本身」相對於父容器的位置,比如把按鈕放在父佈局的靠左、靠右。

六、ImageView 的 scaleType

scaleType 決定圖片如何縮放、移動以匹配 ImageView 的尺寸:

  • CENTER / center:按原尺寸居中顯示,超出部分裁剪居中區域
  • CENTER_CROP / centerCrop:按比例放大鋪滿,使圖片長寬等於或大於 View
  • CENTER_INSIDE / centerInside:完整居中顯示,按比例縮小至不超過 View
  • FIT_CENTER / fitCenter:按比例縮放至適應 View 寬度,居中顯示
  • FIT_END / fitEnd:按比例縮放,靠下顯示
  • FIT_START / fitStart:按比例縮放,靠上顯示
  • FIT_XY / fitXY:不按比例拉伸鋪滿 View
  • MATRIX / matrix:用矩陣繪製,可動態縮放

提醒:drawable 文件夾裡的圖片命名不能使用大寫字母。

七、margin 與 padding 的區別

這對概念也常被混淆:

  • layout_margin:組件距離父容器的距離(外邊距)。
  • padding:組件內容距離組件邊緣的距離(內邊距)。

對應地,layout_gravity 控制組件相對於父容器的位置,而 gravity 控制組件內容的顯示位置。可以這樣歸納:layout_margin 與 layout_gravity 都和佈局有關,決定組件在屏幕中的位置;padding 與 gravity 則決定內容在組件內部的位置。此外,layout_gravity 與 gravity 的取值只能從系統給定的常量中選擇,而 margin 與 padding 可任意指定數值,相對更靈活。

八、Interpolator:動畫的變化節奏

很多人對 interpolator 感到陌生。它定義的是動畫的變化率,讓 alpha、scale、translate、rotate 等基本動畫得以加速、減速、重複等。通俗說,就是「動畫進度」的控制者。它的基類只有一個方法 getInterpolation(float input),把時間軸上的點映射成一個作用於變換的倍數。Android 提供了幾個常用子類:

  • AccelerateDecelerateInterpolator:開始與結束較慢,中間加速
  • AccelerateInterpolator:開始較慢,隨後加速
  • DecelerateInterpolator:開始較快,隨後減速
  • CycleInterpolator:沿正弦曲線循環播放指定次數
  • LinearInterpolator:勻速變化,其變化率為常量 f(x) = x

除此之外,你完全可以自定義 Interpolator 子類,實現拋物線、自由落體等更具物理感的動畫效果。

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