要點:
android:orientation="vertical"垂直線性布局,"horizontal"水平線性布局
android:gravity="top"(buttom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical、clip_horizontal)控制布局中控件的對齊方式。如果是沒有子控件的控件設置此屬性,表示其內容的對齊方式,比如說TextView里面文字的對齊方式;若是有子控件的控件設置此屬性,則表示其子控件的對齊方式,gravity如果需要設置多個屬性值,需要使用“|”進行組合
android:gravity 與 android:layout_gravity的區別
android:gravity是指定本元素的子元素相對它的對齊方式。
android:layout_gravity是指定本元素相對它的父元素的對齊方式。
android:layout_weight="1"通過設置控件的layout_weight屬性以控制各個控件在布局中的相對大小,線性布局會根據該控件layout_weight值與其所處布局中所有控件layout_weight值之和的比值為該控件分配占用的區域。在水平布局的LinearLayout中有兩個Button,這兩個Button的layout_weight屬性值都為1,那么這兩個按鈕都會被拉伸到整個屏幕寬度的一半。如果layout_weight指為0,控件會按原大小顯示,不會被拉伸;對於其余layout_weight屬性值大於0的控件,系統將會減去layout_weight屬性值為0的控件的寬度或者高度,再用剩余的寬度或高度按相應的比例來分配每一個控件顯示的寬度或高度。
例:
布局代碼:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 tools:context=".LinearLayoutActivity" > 7 8 <LinearLayout 9 android:layout_width="match_parent" 10 android:layout_height="match_parent" 11 android:layout_weight="1" 12 android:orientation="horizontal" > 13 14 <Button 15 android:layout_width="wrap_content" 16 android:layout_height="match_parent" 17 android:layout_weight="1" 18 android:background="#aa0000" 19 android:gravity="center_horizontal|center_vertical" 20 android:text="第一列" 21 android:textSize="15sp" > 22 </Button> 23 24 <Button 25 android:layout_width="wrap_content" 26 android:layout_height="match_parent" 27 android:layout_weight="1" 28 android:background="#00aa00" 29 android:gravity="center_horizontal" 30 android:text="第二列" 31 android:textSize="15sp" > 32 </Button> 33 34 <Button 35 android:layout_width="wrap_content" 36 android:layout_height="match_parent" 37 android:layout_weight="1" 38 android:background="#0000aa" 39 android:gravity="center|bottom" 40 android:text="第三列" 41 android:textSize="15sp" > 42 </Button> 43 44 <Button 45 android:layout_width="wrap_content" 46 android:layout_height="match_parent" 47 android:layout_weight="1" 48 android:background="#aaaa00" 49 android:gravity="bottom" 50 android:text="第四列" 51 android:textSize="15sp" > 52 </Button> 53 </LinearLayout> 54 55 <LinearLayout 56 android:layout_width="match_parent" 57 android:layout_height="match_parent" 58 android:layout_weight="1" 59 android:orientation="vertical" > 60 61 <Button 62 android:layout_width="match_parent" 63 android:layout_height="match_parent" 64 android:layout_weight="1" 65 android:gravity="bottom" 66 android:text="第1行" 67 android:textSize="15sp" > 68 </Button> 69 70 <Button 71 android:layout_width="match_parent" 72 android:layout_height="match_parent" 73 android:layout_weight="1" 74 android:gravity="bottom" 75 android:text="第2行" 76 android:textSize="15sp" > 77 </Button> 78 79 <Button 80 android:layout_width="match_parent" 81 android:layout_height="match_parent" 82 android:layout_weight="1" 83 android:gravity="bottom" 84 android:text="第3行" 85 android:textSize="15sp" > 86 </Button> 87 88 <Button 89 android:layout_width="match_parent" 90 android:layout_height="match_parent" 91 android:layout_weight="1" 92 android:gravity="bottom" 93 android:text="第4行" 94 android:textSize="15sp" > 95 </Button> 96 </LinearLayout> 97 98 </LinearLayout>
其它干貨下載資源已放入微信公眾號【一個碼農的日常】