1. 程式人生 > >Android學習筆記10——LinearLayout(線性佈局)

Android學習筆記10——LinearLayout(線性佈局)

##前言
在實際的Andorid開發中,我們肯定經常使用LinearLayout(線性佈局)。下面分享一下,我的學習心得。
##LinearLayout(線性佈局)
LinearLayout是一個檢視組允許所有的子檢視在豎直或水平單一方向上擺放的佈局。

###常用屬性
1.android:orientation
設定佈局管理器內元件的排列方式,可以設定為 horizontal (水平排列)、vertical (垂直排列)。

2.android:layout_width
設定佈局管理器的寬度,可以設定為wrap_content(包裹大小)、match_parent(填充父控制元件)、指定大小(單位dp)。

3.android:layout_height
設定佈局管理器的高度,可以設定為wrap_content(包裹大小)、match_parent(填充父控制元件)、指定大小(單位dp)。

4.android:gravity
設定佈局管理器內元件的對齊方式,該屬性值可設為 top(頂部對齊) 、bottom(底部對齊) 、left(左對齊) 、right(右對齊) 、center_vertical(垂直方向居中) 、 fill_vertical(垂直方向填充) 、 center_horizontal(水平方向居中) 、 fill_horizontal(水平方向填充) 、center(垂直與水平方向都居中) 、 fill (填充)、 clip_vertical(垂直方向裁剪) 、 clip_horizontal(水平方向裁剪) 。
注意:可同時指定多種對其方式的組合,中間用“|”連線,如下方程式碼設定對齊方式為 left|center_vertical 表示出現在螢幕左邊且垂直居中

5.android:layout_gravity
設定view在其父view中的對其方式。 該屬性值可設為 top(頂部對齊) 、bottom(底部對齊) 、left(左對齊) 、right(右對齊) 、center_vertical(垂直方向居中) 、 fill_vertical(垂直方向填充) 、 center_horizontal(水平方向居中) 、 fill_horizontal(水平方向填充) 、center(垂直與水平方向都居中) 、 fill (填充)、 clip_vertical(垂直方向裁剪) 、 clip_horizontal(水平方向裁剪) 。

6.android:padding


設定元素與邊框之間的距離(外間距),這是一次設定四個方向。單獨設定某個方向有另外四個屬性分別是android:paddingTop(元素的上面)、android:paddingBottom(元素的下面)、android:paddingLeft(元素的左邊)、android:paddingRight(元素的右邊)。

7.android:layout_margin
設定相對於父控制元件之間的距離(內間距),這是一次設定四個方向。單獨設定某個方向有另外四個屬性分別是android:layout_marginTopandroid:layout_marginBottomandroid:layout_marginLeftandroid:layout_marginRight

8.android:layout_weight
設定控制元件在父佈局的比重,通過百分百比來進行佈局。需要注意的是,使用weight需要將android:layout_width或者android:layout_height屬性設定為0或0px,不這樣設定可以與你想要的效果不一樣。

9.android:background
設定控制元件的背景,可以是圖片或者顏色。