常用的四种布局资源
Android资源类型中,布局资源(layout/) 用于定义界面布局,即界面的结构和控件排列方式。常用的布局资源有以下四种:线性布局(LinearLayout)、相对布局(RelativeLayout)、表格布局(TableLayout)、帧布局(FrameLayout)。
1.线性布局(LinearLayout)
特点:将子控件按照水平(horizontal) 或垂直(vertical) 方向依次排列。
核心属性:
android:orientation:设置布局内控件的排列顺序。其中vertical表示LinearLayout内控件从上到下依次排列;horizontal表示LinearLayout内控件从左到右依次排列。
android:layout_weight:在布局内设置控件权重,属性值可以直接使用int值。通过设置该属性值,可使布局内的控件按照权重比显示大小。在进行屏幕适配时起到关键作用。
适用场景:
- 简单的线性界面(登录表单:账号 + 密码 + 按钮)
- 列表项、导航栏、垂直 / 水平排列的控件组
示例代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="按钮1"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="按钮2"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="按钮3"/>
</LinearLayout>
运行效果:
2. 相对布局(RelativeLayout)
特点:通过子控件之间的相对位置或与父布局的相对位置来定位。
核心属性:
(1)相对于父布局的属性
- android:layout_centerInParent:控件居中于父布局
- android:layout_centerHorizontal:控件水平居中
- android:layout_centerVertical:控件垂直居中
- android:layout_alignParentTop:控件顶部与父布局顶部对齐
- android:layout_alignParentBottom:控件底部与父布局底部对齐
- android:layout_alignParentLeft:控件左侧与父布局左侧对齐
- android:layout_alignParentRight:控件右侧与父布局右侧对齐
(2)相对于其他控件的属性
- android:layout_below:位于指定控件的下方
- android:layout_above:位于指定控件的上方
- android:layout_toLeftOf:位于指定控件的左侧
- android:layout_toRightOf:位于指定控件的右侧
- android:layout_alignTop:与指定控件的顶部对齐
- android:layout_alignBottom:与指定控件的底部对齐
- android:layout_alignLeft:与指定控件的左侧对齐
- android:layout_alignRight:与指定控件的右侧对齐
适用场景:
- 控件位置不规则的界面(个人中心、详情页)
- 需要减少布局嵌套的场景
示例代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/AppTheme"
android:id="@+id/mainRelativelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="1dp"
>
<Button
android:id="@+id/btn_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"/>
<Button
android:id="@+id/btn_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮2"
android:layout_centerHorizontal="true"
android:layout_marginTop="260dp"/>
<Button
android:id="@+id/btn_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/btn_two"
android:layout_marginLeft="-5dp"
android:layout_marginBottom="151dp"
android:layout_toRightOf="@id/btn_two"
android:text="按钮3" />
</RelativeLayout>
运行效果:
3. 表格布局(TableLayout)
特点: 以表格(行和列)的形式排列控件,通过标签定义每一行,行内的控件自动成为该行的列。
核心属性:
- android:shrinkColumns:指定可收缩的列,当内容超出屏幕时,该列会收缩
- android:stretchColumns:指定可拉伸的列,该列会填满剩余空间
- android:collapseColumns:指定可隐藏的列
TableRow中的常用属性:
- android:layout_column:指定控件所在的列索引(从0开始)
- android:layout_span:指定控件跨多少列
适用场景:
- 设置页面(如系统设置界面)
- 表单输入界面
- 数据表格展示
- 计算器界面
示例代码:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="2" >
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:text="按钮1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:text="按钮2" />
</TableRow>
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:text="按钮3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:text="按钮4" />
</TableRow>
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:text="按钮5" />
</TableRow>
</TableLayout>
运行效果:
4. 帧布局(FrameLayout)
特点: 最简单的布局方式,所有子控件默认叠加在布局的左上角,后添加的控件会覆盖在先添加的控件之上。
核心属性:
- android:layout_gravity:控制子控件在布局内的对齐方式。可选值:center、left、right、top、bottom、center_vertical、center_horizontal等,多个值可用|组合(如right|top表示右上角)
适用场景:
- Fragment容器(如ViewPager的页面容器)
- 图片加文字遮罩效果
- 加载动画层叠显示
- 角标
示例代码:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn_one"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center"
android:background="#ff0000" />
<Button
android:id="@+id/btn_two"
android:layout_width="220dp"
android:layout_height="220dp"
android:layout_gravity="center"
android:background="#00ff00" />
<Button
android:id="@+id/btn_three"
android:layout_width="140dp"
android:layout_height="140dp"
android:layout_gravity="center"
android:background="#0000ff" />
<Button
android:id="@+id/btn_four"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:background="#ff1243" />
<Button
android:id="@+id/btn_five"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:background="#324678" />
</FrameLayout>
运行效果:
5.四种布局对比总结
| 布局类型 | 定位方式 | 优点 | 缺点 | 推荐场景 |
|---|---|---|---|---|
| LinearLayout | 线性排列 | 简单易用、支持权重分配 | 复杂界面嵌套多 | 登录表单、导航栏 |
| RelativeLayout | 相对位置 | 定位灵活、可减少嵌套 | 约束复杂时可读性下降 | 个人中心、详情页 |
| TableLayout | 表格排列 | 结构化好、自动对齐 | 灵活性较低 | 设置页面、表单 |
| FrameLayout | 图层叠加 | 最轻量、渲染最快 | 仅支持简单对齐 | Fragment容器、遮罩层 |