Android布局分别为:
线性布局LinearLayout
相对布局RelativeLayout
绝对布局AbsoluteLayout
表格布局TableLayout
网格布局GridLayout
帧布局FrameLayout
一. LinearLayout(线性布局)
LinearLayout除了继承View/ViewGroup类的所有属性和方法外,还有其特有的XML属性:
1、线性布局具有水平(horizontal)、垂直(vertical)两种布局方式,通过android:orientation="vertical"
控制
2、gravity:指定布局内部视图与本线性布局的对齐方式,取值同layout_gravity,但是layout_gravity:指定该视图与上级视图的对齐方式。
取值有:left、right、top、bottom、center、center_horizontal(水平居中)、center_vertical(垂直居中).
例如:该视图居中于上级视图,内部文字居于右下。
代码:android:gravity="right||bottom"
android:layout_gravity="center"
3、layout_weight:指定当前视图的宽或高占上级线性布局的权重,若指定当前视图在宽度上占的权重,则layout_width设置为0dp。
二. RelativeLayout(相对布局)
相对布局可以让控件相对于兄弟控件和父控件进行布局:
1. 指定视图(ID):
android:layout_alignLeft
当前视图与指定视图左对齐;
android:layout_alignRight
当前视图与指定视图右对齐;
android:layout_alignTop
当前视图与指定视图顶部对齐;
android:layout_alignBottom
当前视图与指定视图底部对齐;
android:layout_above
当前视图在指定视图的上方;
android:layout_below
当前视图在指定视图的下方;
android:layout_toLeftOf
当前视图在指定视图的左边;
android:layout_toRightOf
当前视图在指定视图的右边;
android:layout_alignBaseline
当前视图的baseline与指定视图的baseline对齐;
2.上级视图:
android:layout_centerInParent
当前视图在上级视图的中间
android:layout_centerHorizontal
当前视图在上级视图的水平方向居中
android:layout_centerVertical
当前视图在上级视图的垂直方向居中
android:layout_alignParentLeft
当前视图与上级视图左侧对齐
android:layout_alignParentRight
当前视图与上级视图右侧对齐
android:layout_alignParentTop
当前视图与上级视图顶部对齐
android:layout_alignParentBottom
当前视图与上级视图底部对齐
例如:
<Button
android:id="@+id/bt_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="水平居中" />
<Button
android:id="@+id/bt_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/bt_4"
android:layout_alignRight="@+id/bt_4"
android:text="位于下部,右侧对齐"/>
<Button
android:id="@+id/bt_6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/bt_5"
android:layout_alignLeft="@+id/bt_4"
android:text="位于下部,左侧对齐" />
三. AbsoluteLayout(绝对布局)
通过控件的绝对位置进行控制,但是手机屏幕的差异导致其适应性差
四. TableLayout(表格布局)
表格布局通过TableRow设置行,直接在表格布局中添加控件则占满整行。
属性:
android:shrinkColumns: 设置可伸展的列,该列可以向行方向伸展,最多可占据一整行。
android:stretchColumns: 设置可收缩的列,当该列子控件的内容太多,已经挤满所在行,该子控件的内容将往列方向显示。
android:collapseColumns: 设置要隐藏的列
索引从0开始计算
例如:
android:stretchColumns="0" ---- 第0列可伸展
android:shrinkColumns="1,2" ---- 第1,2列皆可收缩
android:collapseColumns="*" ---- 隐藏所有行
子属性:
android:layout_column: 指定该单元格在第几列显示
android:layout_span: 指定该单元格占据的列数(未指定时为1)
例如:
android:layout_column="1" ---- 该控件显示在第1列
android:layout_span="2" ---- 该控件占据2列
五. GridLayout(网格布局)
网格布局相对于表格布局,其容器中的控件可以跨多行也可以跨多列。
常用属性为:
1、基本的水平、垂直及对齐方式layout_gravity。
2、设置行列:
android:rowCount=“3” ----网格布局有3行
android:columnCount=“3” ----网格布局有3列
3、设置组件位置:
android:layout_row= "1" ----该组件位于第2行
android:layout_column = "2" ----该组件位于第3列
android:layout_rowSpan = "2" ----该组件纵向横跨2行
android:layout_columnSpan = "2" ----该组件横向横跨2列
例:3*3网格布局
代码:可以看出A和B分别为跨两行和跨两列
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:rowCount="3"
android:columnCount="3">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AAAAAAAAAA"
android:layout_gravity="center"
android:textSize="20sp"
android:layout_row="1"
android:layout_column="2"
android:layout_rowSpan="2"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BBBBBBBBBB"
android:textSize="20sp"
android:layout_row="0"
android:layout_column="0"
android:layout_columnSpan="2"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CCCCC"
android:textSize="20sp"
android:layout_row="0"
android:layout_column="2"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DDDDD"
android:textSize="20sp"
android:layout_row="1"
android:layout_column="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EEEEE"
android:textSize="20sp"
android:layout_row="2"
android:layout_column="0"/>
</GridLayout>
六. FrameLayout(帧布局)
帧布局,其下级视图无法指定所处的位置,只能统统从上级视图FrameLayout的左上角开始添加,并且后边添加的子视图会把之前的子视图覆盖掉。一般框架布局使用在需要重叠显示的场合。例如绘图、游戏界面等等。
常见属性:
1、foreground:指定帧布局的前景图像,该图像在布局内部处于最顶层,不会被布局内其他视图覆盖。
2、foregroundGravity:指定前景图像的对齐方式(同gravity)。
例如:(绿色网格为前景图)
代码:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="@drawable/ic_launcher_background"
android:foregroundGravity="right||bottom">
<TextView
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#FF6155"/>
<TextView
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#FF1111"/>
<TextView
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#000000"/>
</FrameLayout>