LinearLayout(线性布局)
布局呈线性的,这种布局在显示组件的时候会默认保持组件之间的间隔以及组件之间的互相对齐。
线性布局显示组件的方式有两种方式:垂直和水平,是通过orientation来设定的。
不管是水平还是垂直线性布局一行(列)只能放置一个控件。
<?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"> <Button android:text="Button" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/button" /> <Button android:text="Button" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/button2" /> <Button android:text="Button" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/button3" /> </LinearLayout>
常用属性
- android:id - 为控件指定相应的ID
- android:text - 指定控件当中显示的文字,需要注意的是,这里尽量使用string.xml
- android:gravity - 指定控件的基本位置,比如说居中,居右等位置(每个组件默认其值为左上角对齐)
- android:textSize - 指定控件当中字体的大小
- android:background - 指定控件所用的背景色,RGB命名法
- android:width - 指定控件的宽度
- android:height - 指定控件的高度
- android:padding - 指定控件的内边距,也就是说控件当中的内容
- android:sigleLine - 如果设置为真的话,则将控件的内容显示在一行当中
padding内边距指的是当前布局与包含的组件之间的边距
layout_margin外边距指的是与其他组件之间的边距
RelativeLayout(相对布局)
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button1" android:id="@+id/btn1" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Button2" android:id="@+id/btn2" android:layout_below="@id/btn1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button3" android:id="@+id/btn3" android:layout_below="@id/btn2" android:layout_alignRight="@id/btn2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button4" android:id="@+id/btn4" android:layout_below="@id/btn3" android:layout_alignParentRight="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button5" android:id="@+id/btn5" android:layout_below="@id/btn4" android:layout_centerHorizontal="true" /> </RelativeLayout>
常用属性
RelativeLayout布局
- android:gravity="left" //空间布局位置
- android:layout_margin
子类控件相对子类控件:值是另外一个控件的id
- android:layout_above----------位于给定DI控件之上
- android:layout_below ----------位于给定DI控件之下
- android:layout_toLeftOf -------位于给定控件左边
- android:layout_toRightOf ------位于给定控件右边
- android:layout_alignLeft -------左边与给定ID控件的左边对齐
- android:layout_alignRight ------右边与给定ID控件的右边对齐
- android:layout_alignTop -------上边与给定ID控件的上边对齐
- android:layout_alignBottom ----底边与给定ID控件的底边对齐
- android:layout_alignBaseline----对齐到控件基准线
相对父容器,值是true或false
- android:layout_alignParentLeft ------相对于父靠左
- android:layout_alignParentTop-------相对于父靠上
- android:layout_alignParentRight------相对于父靠右
- android:layout_alignParentBottom ---相对于父靠下
- android:layout_centerInParent="true" -------相对于父即垂直又水平居中
- android:layout_centerHorizontal="true" -----相对于父即水平居中
- android:layout_centerVertical="true" --------相对于父即处置居中
相对于父容器位置:
- android:layout_margin="10dp"
- android:layout_marginLeft
- android:layout_marginRight
- android:layout_marginTop
- android:layout_marginBottom
版本4.2以上相对布局新属性
- android:layout_alignStart---------------------将控件对齐给定ID控件的头部
- android:layout_alignEnd----------------------将控件对齐给定ID控件的尾部
- android:layout_alignParentStart--------------将控件对齐到父控件的头部
- android:layout_alignParentEnd---------------将控件对齐到父控件的尾部
TableLayout(表格布局)
顾名思义就是像表格一样布局,通常情况下,TableLayout有多个TableRow组成,每个TableRow就是一行。
比较少用,可以用GridView替代。
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TableRow> <Button android:text="Button1"/> <Button android:text="Button2"/> <Button android:text="Button3"/> </TableRow> <TableRow> <Button android:text="Button4"/> <Button android:text="Button5"/> <Button android:text="Button6"/> </TableRow> <TableRow> <Button android:text="Button7"/> <Button android:text="Button8"/> <Button android:text="Button9"/> </TableRow> </TableLayout>
常用属性
TableLayout可设置的属性包括全局属性及单元格属性。
1、全局属性也即列属性,有以下3个参数:
- android:stretchColumns 设置可伸展的列。该列可以向行方向伸展,最多可占据一整行。
- android:shrinkColumns 设置可收缩的列。当该列子控件的内容太多,已经挤满所在行,那么该子控件的内容将往列方向显示。
- android:collapseColumns 设置要隐藏的列。
示例:
android:stretchColumns="0" 第0列可伸展
android:shrinkColumns="1,2" 第1,2列皆可收缩
android:collapseColumns="*" 隐藏所有行
说明:列可以同时具备stretchColumns及shrinkColumns属性,若此,那么当该列的内容N多时,将“多行”显示其内容。(这里不是真正的多行,而是系统根据需要自动调节该行的layout_height)
2、单元格属性,有以下2个参数:
- android:layout_column 指定该单元格在第几列显示
- android:layout_span 指定该单元格占据的列数(未指定时,为1)
示例:
android:layout_column="1" 该控件显示在第1列
android:layout_span="2" 该控件占据2列
说明:一个控件也可以同时具备这两个特性
AbsoluteLayout(绝对布局)
指的是指定组件的左上角绝对坐标来指定组件的布局。
已过时,不推荐使用。
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="Button1" android:layout_x="100dp" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Button2" android:layout_y="100dp" /> </AbsoluteLayout>
FrameLayout(单帧布局)
所有控件位于左上角,并且直接覆盖前面的子元素。
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Button1" /> <Button android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="Button2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button3" /> </FrameLayout>
常用属性
- android:foreground:设置该帧布局容器的前景图像
- android:foregroundGravity:设置前景图像显示的位置
GridLayout(网格布局)
GridLayout是Android4.0新提供的网格矩阵形式的布局控件。
<?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:rowCount="2" android:columnCount="3" > <Button android:id="@+id/one" android:layout_column="1" android:text="1"/> <Button android:id="@+id/two" android:layout_column="0" android:text="2"/> <Button android:id="@+id/three" android:text="3"/> <Button android:id="@+id/devide" android:text="/"/> </GridLayout>
常用属性
android:alignmentMode
属性说明:当设置alignMargins,使视图的外边界之间进行校准。可以取以下值:
alignBounds -- 对齐子视图边界。
alignMargins -- 对齐子视图边距。
android:columnCount
属性说明:GridLayout的最大列数
android:rowCount
属性说明:GridLayout的最大行数
android:columnOrderPreserved
属性说明: 当设置为true,使列边界显示的顺序和列索引的顺序相同。默认是true。
android:orientation
属性说明:GridLayout中子元素的布局方向。有以下取值:
horizontal -- 水平布局。
vertical -- 竖直布局。
android:rowOrderPreserved
属性说明:当设置为true,使行边界显示的顺序和行索引的顺序相同。默认是true。
android:useDefaultMargins
属性说明: 当设置ture,当没有指定视图的布局参数时,告诉GridLayout使用默认的边距。默认值是false。