Android基礎TOP3:線性布局的特點,常用屬性,及權重值,androidtop3
線性布局是一種讓視圖水平或者垂直布排列的布局;
常用屬性:
androuid:orientation :表示布局方向
- 取值vertical表示垂直布局
- 取值horizontal表示水平布局
android:gravity 表示視圖對齊
- 內容包括 TOP,bottom,left,right,center_vertical,center_horizontal,center
- 可以使用“|”分割填寫多個值
布局中的視圖可以使用如下多個屬性:
android:layout_gravity 表示單個視圖的對齊
android:layout_weight 表示單個視圖所在大小的比重
- 當Layout_weight為0時候視圖大小自身確定
- 當layout_weight大於0時,視圖線上性布局方向根據比重展開
代碼示範:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="horizontal" > 6 <Button 7 android:layout_width="200dp" 8 android:layout_height="100dp" 9 android:text="adaflkjn"10 android:gravity="bottom|center_horizontal"/>11 12 </LinearLayout>
android:gravity:是決定控制項內元素在某個位置
<Button android:layout_width="200dp" android:layout_height="100dp" android:text="adaflkjn" android:layout_gravity="center"/>
android:layout_gravity是本元素在父元素裡面顯示的位置
weight的應用
<EditText android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="sdaf"/> <Button android:layout_width="1dp" android:layout_height="wrap_content" android:layout_weight="0" android:text="klndgjl" />
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0" android:text="weight為0" android:background="#FFF0F5" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="weight為1" android:layout_weight="1" android:background="#800080" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="weight為4" android:layout_weight="4" android:background="#4B0082" />