要點(diǎn):
android:orientation="vertical"垂直線性布局,"horizontal"水平線性布局
android:gravity="top"(buttom、left冈涧、right、center_vertical正蛙、fill_vertical督弓、center_horizontal、fill_horizontal乒验、center愚隧、fill、clip_vertical徊件、clip_horizontal)控制布局中控件的對齊方式奸攻。如果是沒有子控件的控件設(shè)置此屬性,表示其內(nèi)容的對齊方式虱痕,比如說TextView里面文字的對齊方式睹耐;若是有子控件的控件設(shè)置此屬性,則表示其子控件的對齊方式部翘,gravity如果需要設(shè)置多個(gè)屬性值硝训,需要使用“|”進(jìn)行組合
android:gravity 與 android:layout_gravity的區(qū)別
android:gravity是指定本元素的子元素相對它的對齊方式。
android:layout_gravity是指定本元素相對它的父元素的對齊方式新思。
android:layout_weight="1"通過設(shè)置控件的layout_weight屬性以控制各個(gè)控件在布局中的相對大小,線性布局會根據(jù)該控件layout_weight值與其所處布局中所有控件layout_weight值之和的比值為該控件分配占用的區(qū)域窖梁。在水平布局的LinearLayout中有兩個(gè)Button,這兩個(gè)Button的layout_weight屬性值都為1夹囚,那么這兩個(gè)按鈕都會被拉伸到整個(gè)屏幕寬度的一半纵刘。如果layout_weight指為0,控件會按原大小顯示荸哟,不會被拉伸假哎;對于其余l(xiāng)ayout_weight屬性值大于0的控件瞬捕,系統(tǒng)將會減去layout_weight屬性值為0的控件的寬度或者高度,再用剩余的寬度或高度按相應(yīng)的比例來分配每一個(gè)控件顯示的寬度或高度舵抹。
#源代碼
'''<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
??? xmlns:tools="http://schemas.android.com/tools"
??? android:layout_width="match_parent"
??? android:layout_height="match_parent"
??? android:orientation="vertical"
??? tools:context=".LinearLayoutActivity" >
?<LinearLayout
??????? android:layout_width="match_parent"
??????? android:layout_height="match_parent"
??????? android:layout_weight="1"
??????? android:orientation="horizontal" >
<Button
??????????? android:layout_width="wrap_content"
??????????? android:layout_height="match_parent"
??????????? android:layout_weight="1"
??????????? android:background="#aa0000"
??????????? android:gravity="center_horizontal|center_vertical"
??????????? android:text="第一列"
??????????? android:textSize="15sp" >
??????? </Button>
??????? <Button
??????????? android:layout_width="wrap_content"
??????????? android:layout_height="match_parent"
??????????? android:layout_weight="1"
??????????? android:background="#00aa00"
??????????? android:gravity="center_horizontal"
??????????? android:text="第二列"
??????????? android:textSize="15sp" >
??????? </Button>
??????? <Button
??????????? android:layout_width="wrap_content"
??????????? android:layout_height="match_parent"
??????????? android:layout_weight="1"
??????????? android:background="#0000aa"
??????????? android:gravity="center|bottom"
??????????? android:text="第三列"
??????????? android:textSize="15sp" >
??????? </Button>
??????? <Button
??????????? android:layout_width="wrap_content"
??????????? android:layout_height="match_parent"
??????????? android:layout_weight="1"
??????????? android:background="#aaaa00"
??????????? android:gravity="bottom"
??????????? android:text="第四列"
??????????? android:textSize="15sp" >
??????? </Button>
??? </LinearLayout>
??? <LinearLayout
??????? android:layout_width="match_parent"
??????? android:layout_height="match_parent"
??????? android:layout_weight="1"
??????? android:orientation="vertical" >
??????? <Button
??????????? android:layout_width="match_parent"
??????????? android:layout_height="match_parent"
??????????? android:layout_weight="1"
??????????? android:gravity="bottom"
??????????? android:text="第1行"
??????????? android:textSize="15sp" >
??????? </Button>
??????? <Button
??????????? android:layout_width="match_parent"
??????????? android:layout_height="match_parent"
??????????? android:layout_weight="1"
??????????? android:gravity="bottom"
??????????? android:text="第2行"
??????????? android:textSize="15sp" >
??????? </Button>
??????? <Button
??????????? android:layout_width="match_parent"
??????????? android:layout_height="match_parent"
??????????? android:layout_weight="1"
??????????? android:gravity="bottom"
??????????? android:text="第3行"
??????????? android:textSize="15sp" >
??????? </Button>
??????? <Button
??????????? android:layout_width="match_parent"
??????????? android:layout_height="match_parent"
??????????? android:layout_weight="1"
??????????? android:gravity="bottom"
??????????? android:text="第4行"
??????????? android:textSize="15sp" >
??????? </Button>
??? </LinearLayout>
</LinearLayout>'''
'''<Button
??????????? android:layout_width="wrap_content"
??????????? android:layout_height="match_parent"
??????????? android:layout_weight="1"
??????????? android:background="#aa0000"
??????????? android:gravity="center_horizontal|center_vertical"
??????????? android:text="第一列"
??????????? android:textSize="15sp" >
??????? </Button>
??????? <Button
??????????? android:layout_width="wrap_content"
??????????? android:layout_height="match_parent"
??????????? android:layout_weight="1"
??????????? android:background="#00aa00"
??????????? android:gravity="center_horizontal"
??????????? android:text="第二列"
??????????? android:textSize="15sp" >
??????? </Button>
??????? <Button
??????????? android:layout_width="wrap_content"
??????????? android:layout_height="match_parent"
??????????? android:layout_weight="1"
??????????? android:background="#0000aa"
??????????? android:gravity="center|bottom"
??????????? android:text="第三列"
??????????? android:textSize="15sp" >
??????? </Button>
??????? <Button
??????????? android:layout_width="wrap_content"
??????????? android:layout_height="match_parent"
??????????? android:layout_weight="1"
??????????? android:background="#aaaa00"
??????????? android:gravity="bottom"
??????????? android:text="第四列"
??????????? android:textSize="15sp" >
??????? </Button>
??? </LinearLayout>
??? <LinearLayout
??????? android:layout_width="match_parent"
??????? android:layout_height="match_parent"
??????? android:layout_weight="1"
??????? android:orientation="vertical" >
??????? <Button
??????????? android:layout_width="match_parent"
??????????? android:layout_height="match_parent"
??????????? android:layout_weight="1"
??????????? android:gravity="bottom"
??????????? android:text="第1行"
??????????? android:textSize="15sp" >
??????? </Button>
??????? <Button
??????????? android:layout_width="match_parent"
??????????? android:layout_height="match_parent"
??????????? android:layout_weight="1"
??????????? android:gravity="bottom"
??????????? android:text="第2行"
??????????? android:textSize="15sp" >
??????? </Button>
??????? <Button
??????????? android:layout_width="match_parent"
??????????? android:layout_height="match_parent"
??????????? android:layout_weight="1"
??????????? android:gravity="bottom"
??????????? android:text="第3行"
??????????? android:textSize="15sp" >
??????? </Button>
??????? <Button
??????????? android:layout_width="match_parent"
??????????? android:layout_height="match_parent"
??????????? android:layout_weight="1"
??????????? android:gravity="bottom"
??????????? android:text="第4行"
??????????? android:textSize="15sp" >
??????? </Button>
??? </LinearLayout>
</LinearLayout>'''