1. 程式人生 > >安卓學習記錄——(2.介面佈局)

安卓學習記錄——(2.介面佈局)

LinearLayout(線性佈局)

常用屬性

orientation:佈局中元件的排列方式,分為horizontal(水平),vertical(豎直)。

gravity:元件包含的子元素對齊方式,可多個組合。如(left|buttom)

layout_gravity:控制該元件在父容器裡的對齊方式。

layout_width:佈局寬度,wrap_content(元件實際大小),fill_parent或match_parent(填滿父容器)
後兩種佈局寬度的差別
fill_parent和match_parent其實一樣的使用,在andriod2.2開始兩個
是一樣的,只是考慮到如果安卓版本太低,需要使用fill_parent

layout_height:佈局的高度,和layout_width引數相同。

id:該元件的一個資源id,在java檔案中通過findViewById(id)找到對應的元件。

backgroud:設定一個背景圖片。

Weight(權重)

用於等比例地劃分區域,通過相對的比例設定佈局。

divider分割線

非常用屬性,用於為LinearLayout設定分割線圖片,通過showDividers來設定分割線的所在位置,有none,middle,begining,end四個選項。
其他屬性還有:
dividerPadding:設定分割線的padding

參考連結

RelativeLayout(相對佈局)

基本屬性

gravity:容器對齊方式

ignoreGravity:設定為true時,不受gravity屬性的影響。

根據父容器定位

layout_alignParentLeft:左對齊
layout_alignParentRight:右對齊
layout_alignParentTop:頂部對齊
layout_alignParentBottom:底部對齊

layout_centerHorizontal:水平居中
layout_centerVertical:垂直居中
layout_centerInParent:中間位置

根據兄弟元件定位

layout_toLeftOf:參考元件的左邊
layout_toRightOf:參考元件的右邊
layout_above:參考元件的上方
layout_below:參考元件的下方
layout_alignTop:對齊參考元件的上邊界,同理對應Bottom,Left,Right

margin(偏移)

layout_margin:設定元件上下左右的偏移量
layout_marginLeft:設定元件左邊的偏移量,同理對應Right,Top,Buttom

padding(填充)

padding:往內部元素的上下左右填充一定邊距
paddingLeft:往內部元素的左邊填充一定邊距,同理對應Right,Top,Bottom

參考連結

TableLayout(表格佈局)

常用屬性

collapseColumns:隱藏列
stretchColumns:拉伸列
shrinkColumns:收縮列

確定行數

參考連結

FrameLayout(幀佈局)

常用屬性

android:foreground:設定前景影象
android:foregroundGravity:設定前景影象顯示位置

參考連結

GridLayout(網格佈局)

設定排列對齊

android:orientation=”” vertical(豎直)|horizontal(水平)
android:layout_gravity=”” center,left,right,buttom可以同時使用多種

設定行列

行數android:rowCount=””
列數android:columnCount=”“

設定元件

android:layout_row:設定第幾行
android:layout_column:設定第幾列

設定元件橫跨行列數

android:layout_rowSpan:設定橫跨行數
android:layout_columnSpan:設定橫跨列數

參考連結

AbsoluteLayout(絕對佈局)

佈局位置固定。所有元件的屬性需要固定。