3. 帧布局 FrameLayout
帧布局的特点为从父容器的左上角开始绘制布局,因此所有位于帧布局之内的子控件或者容器之间可以互相覆盖。
1. 标签 <FrameLayout />
2. 基本属性
- id :表示当前布局或控件的唯一标识,构建后自动在R.java文件中生成一串标识符,可作为查找和引用控件的参考。
- layout_width:表示当前布局的宽度,可以使用 match_parent 或 wrap_content 来表示当前的布局大小,或直接使用dp值固定大小。
- layout_height :表示当前布局的高度,可以使用 match_parent 或 wrap_content 来表示当前的布局大小,或直接使用dp值固定大小。
- background :表示当前布局的背景,可以用十六进制的RGB、ARGB 或 RRGGBB、AARRGGBB来表示,也可以调用 drawble 文件夹当中的图片作为背景。
- padding : 表示当前布局内部空间的填充值,一般使用dp来作为衡量单位,线性布局从左上角开始,也可以选择方向paddingTop,paddingLeft,paddingRight,paddingBottom。
- margin : 表示当前控件或布局距离周围空间布局或父布局的距离,可选方向为margin_left,margin_top,margin_bottom,margin_right。
- forground:表示当前帧布局中控件的前景,可以是十六进制颜色或图片,永远显示在当前控件的最上层。
- forground_gravity:表示当前控件中前景的布局位置,取值可以是 top\right\bottom等。
- layout_gravity: 用来处理当前帧布局中子控件位于帧布局当中的位置,因此此属性一定处于子控件的标签中。
3. 代码实例
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FrameLayout"
android:foreground="@drawable/ic_launcher_background"
android:foregroundGravity="center">
<TextView
android:layout_width="300dp"
android:layout_height="300dp"
android:background="#f00"
></TextView>
<TextView
android:layout_width="250dp"
android:layout_height="250dp"
android:background="#ff453216"
></TextView>
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#f66565"
></TextView>
</FrameLayout>
4. 表格布局 TableLayout
表格布局的特点是控件会直接在布局中占用一行的大小,但和线性布局有差别,使用一个button控件在表格布局中和在线性水平布局中是不一样的,属性的宽高可设置为wrap_content。另外如果要多行显示可以使用标签<TableRow >并将每行中的控件放置其中。缺点是布局比较呆板,适用于计算器类的界面。
1. 标签:<TableLayout >
2. 基本属性:
- id :表示当前布局或控件的唯一标识,构建后自动在R.java文件中生成一串标识符,可作为查找和引用控件的参考。
- layout_width:表示当前布局的宽度,可以使用 match_parent 或 wrap_content 来表示当前的布局大小,或直接使用dp值固定大小。
- layout_height :表示当前布局的高度,可以使用 match_parent 或 wrap_content 来表示当前的布局大小,或直接使用dp值固定大小。
- background :表示当前布局的背景,可以用十六进制的RGB、ARGB 或 RRGGBB、AARRGGBB来表示,也可以调用 drawble 文件夹当中的图片作为背景。
- padding : 表示当前布局内部空间的填充值,一般使用dp来作为衡量单位,线性布局从左上角开始,也可以选择方向paddingTop,paddingLeft,paddingRight,paddingBottom。
- margin : 表示当前控件或布局距离周围空间布局或父布局的距离,可选方向为margin_left,margin_top,margin_bottom,margin_right。
- layout_column/ layout_span :子控件跨列显示以及跳列显示.表格布局两行以上才有效果
- stretch_column: 拉伸某列,取值为int,其中下标为0开始表示第1列,控件没有占满屏幕生效
- shrink_column: 收缩某列,取值同上,控件溢出屏幕生效
- collapse_column:隐藏某列,取值同上
3. 代码实例:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TableLayout">
<TableRow>
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button1"
></Button>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button2"
android:layout_column="2"
></Button>
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button3"></Button>
</TableRow>
<TableRow>
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button1"
></Button>
<Button
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button2"
></Button>
<Button
android:id="@+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button3"
android:layout_span="2"></Button>
</TableRow>
</TableLayout>
5. 网格布局 GridLayout
1. 标签 <GridLayout />
2. 基本属性:
id :表示当前布局或控件的唯一标识,构建后自动在R.java文件中生成一串标识符,可作为查找和引用控件的参考。
layout_width:表示当前布局的宽度,可以使用 match_parent 或 wrap_content 来表示当前的布局大小,或直接使用dp值固定大小。
layout_height :表示当前布局的高度,可以使用 match_parent 或 wrap_content 来表示当前的布局大小,或直接使用dp值固定大小。
background :表示当前布局的背景,可以用十六进制的RGB、ARGB 或 RRGGBB、AARRGGBB来表示,也可以调用 drawble 文件夹当中的图片作为背景。
padding : 表示当前布局内部空间的填充值,一般使用dp来作为衡量单位,线性布局从左上角开始,也可以选择方向paddingTop,paddingLeft,paddingRight,paddingBottom。
margin : 表示当前控件或布局距离周围空间布局或父布局的距离,可选方向为margin_left,margin_top,margin_bottom,margin_right。
*orientation:*设置排列方式水平或垂直
*layout_gravity:*设置对其方式,左右上下居中,如需要同时使用两个方向可以使用
|
,例如left|bottom
*rowcount:*设置行数,赋值为数字,例如
rowcount = 4
则有4行*columncount:*设置列数,用法同上
控件属性:
*layout_row :*设置控件位于第几行,赋值为数字,例如
layout_row = 2
则控件位于第3行(从0开始计算)layout_column : 设置控件位于第几列,用法同上
layout_rowSpan: 设置控件纵向跨几行,赋值为数字,例如
layout_rowSpan = 4
则控件纵向跨4行layout_columnSpan: 设置控件横向跨几列,用法同上
3. 代码实例:
<?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".GridLyout" android:orientation="horizontal" android:columnCount="4" android:rowCount="4"> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button1" android:layout_row="4" android:layout_column="2" ></Button> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button2" android:layout_rowSpan="2"></Button> <Button android:id="@+id/btn3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button3"></Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button4"></Button> <Button android:id="@+id/btn5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button5" android:layout_columnSpan="2" ></Button> <Button android:id="@+id/btn6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button6" android:layout_rowSpan="2"></Button> <Button android:id="@+id/btn7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button7"></Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button8"></Button> </GridLayout>