1.主要的五種布局:
線性布局 - Linear Layout:按排列的方向(orientation)分為水平線性布局(
android:orientation="vertical"
)和垂直線性布局(
android:orientation="horizontal"//默認的是水平線性布局
)。
線性布局定義了排列方向后,會一直沿着該方向一直排列下去,除非利用嵌套再重新定義。
如下圖所示,可以看做由多個水平線性布局組合而成的垂直線性布局。
常用代碼:
android:layout_weight="數字" //表示剩余空間該控件所占的百分比,通常用於平均幾個控件之間的位置,定義為1
注意: 區分“android:gravity”和“android:layout_gravity”。
android:gravity:是對控件本身來說的,是用來設置“控件自身的內容”應該顯示在“控件自身體積”的什么位置,默認值是左側。
android:layout_gravity:是相對於控件的父元素來說的,設置該控件在它的父元素的什么位置。
拓展連接:
相對布局 - Relative Layout:
android:layout_alignParentLeft="true" 位於父容器左上角
android:layout_alignParentBottom, android:layout_alignParentTop,
android:layout_alignParentRight 只能在父控件為RelativeLayout時才起作用,而對於像LinearLayout這樣的布局不起作用
android:layout_centerInParent="true" 位於布局容器的中央位置;
layout_centerHorizontal位於布局容器水平居中位置;
layout_centerVertical位於布局容器垂直居中位置
被參照控件:控件與控件之間位置
android:layout_below="@id/***" 位於***組件下方
android:layout_toLeftOf="@id/###"位於###組件左則,緊貼並列
控件與控件之間對齊方式
android:layout_alignLeft="@id/***"與***組件左邊界緊貼對齊,疊在一起;
android:layout_alignTop="@id/###"與###組件上邊界對齊
可以通過另一個控件的ID來確定當前控件的位置(即任意兩個控件之間的相對位置)。
android:layout_marginTop=“25dip” //頂部距離 android:gravity=“left” //控件中文本位置 android:layout_marginLeft="15dip //距離左邊距 // 相對於給定ID控件 android:layout_above 將該控件的底部置於給定ID的控件之上(將該控件置於指定控件的上面); android:layout_below 將該控件的頂部置於給定ID的控件之下(將該控件置於指定控件的下面);
表格布局 - Table Layout:
表格布局模型以行列的形式管理子控件,每一行為一個TableRow的對象,當然也可以是一個View的對象。TableRow可以添加子控件,每添加一個為一列。N個TableRow之間是以N行的行數的方式縱向排列。
android:shrinkColumns="n”:設置第n+1列為可收縮的列。當可收縮的列太寬(內容過多)不會被擠出屏幕。當需要設置多列為可收縮時,將列序號用逗號隔開。
android:stretchColumns="n”:設置第n+1列為可伸展的列,以填滿剩下的多余空白空間,若有多列需要設置為可伸展,請用逗號將需要伸展的列序號隔開。
列元素(Button)屬性,定義子控件。
android:layout_colum:設置該控件在TableRow中指定的列。
android:layout_span:設置該控件所跨越的列數。
<TableRow/>
<Button
android:layout_weight="wrap_content"
android:layout_height="wrap_content"
android:text="為什么"/>
<Button
android:layout_weight="wrap_content"
android:layout_height="wrap_content"
android:text="你在這"/>
網格布局—Graid Layout:
網格布局是水平線性布局的,當控件水平方向達到定義的列數時會自動跳行
以下是一個計算器的例子:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <GridLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:rowCount="5" //定義行最大格數為5,列最大格數為4的格子 android:columnCount="4" > <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:textSize="40dp" android:layout_columnSpan="4" android:layout_gravity="fill" />//定義計算器的輸入框 <Button android:text="1" android:textSize="30dp"/> <Button android:text="2" android:textSize="30dp"/> <Button android:text="3" android:textSize="30dp"/> <Button android:text="/" android:textSize="30dp"/> <Button android:text="4" android:textSize="30dp"/> <Button android:text="5" android:textSize="30dp"/> <Button android:text="6" android:textSize="30dp"/> <Button android:text="*" android:textSize="30dp"/> <Button android:text="7" android:textSize="30dp"/> <Button android:text="8" android:textSize="30dp"/> <Button android:text="9" android:textSize="30dp"/> <Button android:text="=" android:textSize="30dp" android:layout_rowSpan="3"//表示=的該格子占了3行 android:layout_gravity="fill"//如果需要往水平或豎直方向填充該控件,這句話不寫,會多空出兩個格子,但不會充滿三個格子/> <Button android:text="0" android:textSize="30dp" android:layout_columnSpan="2" android:layout_gravity="fill"/> //該格子占了兩列 <Button android:text="." android:textSize="30dp" android:layout_rowSpan="2" android:layout_gravity="fill" /> <Button android:text="+" android:textSize="30dp"/> <Button android:text="-" android:textSize="30dp"/> </GridLayout> </LinearLayout>
結果圖:
絕對布局 - AbsoluteLayout:
絕對布局的子控件需要指定相對於此坐標布局的橫縱坐標值,手機應用需要適應不同的屏幕大小,而這種布局模型不能自適應屏幕尺寸大小,所以應用的相對較少。
android:layout_x="10px"水平絕對位置
android:layout_y="10px"垂直絕對位置
幀布局 – FrameLayout:
幀布局,默認針對左邊位置的(可用
android:layout_gravity="center"定義位置
)。所有的控件都有各自的坐標位置來顯示-->
又框架布局是最簡單的布局形式。所有添加到這個布局中的視圖都以層疊的方式顯示。第一個添加的控件被放在最底層,最后一個添加到框架布局中的視圖顯示在最頂層,上一層的控件會覆蓋下一層的控件。這種顯示方式有些類似於堆棧。