android资源类型包括图片资源、主题与样式资源、布局资源、字符串资源、颜色资源、尺寸资源。 Android 布局分为传统 ViewGroup 布局和现代约束布局,最常用的 4 种为:
- LinearLayout(线性布局)
- RelativeLayout(相对布局)
- FrameLayout(帧布局)
- TableLayout(表格布局)
1. LinearLayout(线性布局)
核心特性
- 最基础、最简单的布局,所有子控件按「水平 / 垂直」方向排成一条直线
- 子控件不会重叠,依次排列
- 支持权重(
layout_weight)实现按比例分配空间(适配神器)
关键属性
表格
| 属性 | 作用 |
|---|---|
android:orientation | 排列方向:horizontal(水平)/vertical(垂直) |
android:layout_weight | 子控件权重,按比例分配剩余空间 |
android:gravity | 子控件在布局内的对齐方式(居中、居左、居右) |
代码示例
xml
<?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="vertical" <!-- 垂直排列 -->
android:gravity="center_horizontal"> <!-- 子控件水平居中 -->
<!-- 权重1:占1/3高度 -->
<Button
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="按钮1"/>
<!-- 权重2:占2/3高度 -->
<Button
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="2"
android:text="按钮2"/>
</LinearLayout>
适用场景
- 简单的线性界面(登录表单、列表项、横向按钮栏)
- 需要按比例分配宽 / 高的场景
2. RelativeLayout(相对布局)
核心特性
- 以「子控件之间的相对位置」或「父容器的相对位置」定位
- 无需嵌套,一个布局就能实现复杂排版
- 灵活性高,但层级复杂时可读性差
关键属性
- 相对于父容器:
layout_centerInParent(居中)、layout_alignParentTop(顶部对齐) - 相对于其他控件:
layout_below(在某控件下方)、layout_toRightOf(在某控件右侧)
代码示例
xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 父容器居中 -->
<TextView
android:id="@+id/tv_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="居中文本"/>
<!-- 在居中文本上方 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/tv_center"
android:text="上方按钮"/>
</RelativeLayout>
适用场景
- 控件之间有相对依赖关系的界面(个人中心、详情页)
- 减少布局嵌套层级
3. FrameLayout(帧布局)
核心特性
- 最简单的布局,所有子控件默认「堆叠在左上角」
- 后添加的控件会覆盖先添加的控件(层级覆盖)
- 测量 / 渲染速度最快,性能最优
关键属性
android:layout_gravity:控制子控件在布局内的对齐(居中、底部、右侧)- 无复杂排列规则,仅支持简单对齐
代码示例
xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 底层图片 -->
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher"/>
<!-- 上层居中文字,覆盖在图片上 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="覆盖文字"
android:textSize="20sp"/>
</FrameLayout>
适用场景
- 图层叠加(图片 + 水印、按钮 + 角标)
- Fragment 容器、ViewPager 页面、加载动画遮罩
4.TableLayout(表格布局)
核心特性
-
以行和列的形式排列控件,一行一行、一列一列对齐
-
由
TableLayout+TableRow组成:TableLayout:整个表格容器TableRow:表格里的每一行
-
列数由包含控件最多的那一行决定
-
列宽会自动适配内容,也可以手动设置隐藏 / 拉伸
-
不用手动定义列,自动对齐,非常适合规整的表格界面
关键属性
表格
| 属性 | 作用 |
|---|---|
android:stretchColumns | 设置可拉伸的列(填满表格剩余宽度,用列号表示,从 0 开始) |
android:shrinkColumns | 设置可收缩的列(内容过长时自动收缩) |
android:collapseColumns | 设置隐藏的列 |
TableRow | 代表表格中的一行,内部控件就是一列 |
代码示例(用户信息表格)
xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 表格布局:第0列收缩,第1列拉伸 -->
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="0" <!-- 第0列可收缩 -->
android:stretchColumns="1"> <!-- 第1列可拉伸 -->
<!-- 第一行 -->
<TableRow>
<TextView android:text="姓名:"/>
<TextView android:text="张三"/>
</TableRow>
<!-- 第二行 -->
<TableRow>
<TextView android:text="年龄:"/>
<TextView android:text="25"/>
</TableRow>
<!-- 第三行 -->
<TableRow>
<TextView android:text="联系方式:"/>
<TextView android:text="13800138000"/>
</TableRow>
</TableLayout>
效果
所有行的两列都会严格对齐,左边标签固定宽度,右边内容自动拉伸填满,非常整齐。
适用场景
- 设置界面(选项 + 说明)
- 个人信息、表单界面
- 订单详情、商品参数
- 规整的多行多列列表