1. LinearLayout(线性布局)
特点
1方向控制 :通过 android:orientation 属性设置为 horizontal (水平)或 vertical (垂直)
2权重分配 :使用 android:layout_weight 属性按比例分配空间
3.简单直接 :子元素按照设定方向依次排列
示例代码
<LinearLayout xmlns:android="http://
schemas.android.com/apk/res/android"
android:layout_width="match_pare
nt"
android:layout_height="match_par
ent"
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(相对布局)
特点
- 相对定位 :子元素可以相对于父容器或其他子元素定位
- 灵活性高 :支持多种相对位置属性
- 布局复杂 :可以实现更复杂的界面结构
主要定位属性
- android:layout_alignParentTop/Bottom/Left/Right :相对于父容器边缘
- android:layout_centerHorizontal/Vertical :水平或垂直居中
- android:layout_toLeftOf/RightOf/Above/Below :相对于其他元素
- android:layout_alignTop/Bottom/Left/Right :与其他元素对齐
示例代码
<RelativeLayout
xmlns:android="http://schemas.
android.com/apk/res/android"
android:layout_width="match_pare
nt"
android:layout_height="match_par
ent">
<Button
android:id="@+id/btn_one"
android:layout_width="wrap_c
ontent"
android:layout_height="wrap_
content"
android:text="按钮1"
android:layout_alignParentBo
ttom="true"
android:layout_marginBottom=
"20dp"/>
<Button
android:id="@+id/btn_two"
android:layout_width="wrap_c
ontent"
android:layout_height="wrap_
content"
android:text="按钮2"
android:layout_centerHorizon
tal="true"
android:layout_marginTop="26
0dp"/>
<Button
android:id="@+id/btn_three"
android:layout_width="wrap_c
ontent"
android:layout_height="wrap_
content"
android:layout_alignBottom="
@id/btn_two"
android:layout_toRightOf="@i
d/btn_two"
android:text="按钮3" />
</RelativeLayout>
适用场景
- 复杂的界面布局
- 需要精确定位元素的场景
- 元素之间有相对位置关系的界面
3. TableLayout(表格布局)
特点
- 表格结构 :使用行(TableRow)和列组织元素
- 列宽控制 :通过 android:stretchColumns 等属性控制列的宽度
- 单元格合并 :支持跨列显示
主要属性
- android:stretchColumns :设置可拉伸的列
- android:shrinkColumns :设置可收缩的列
- android:collapseColumns :设置可折叠的列
示例代码
<TableLayout xmlns:android="http://
schemas.android.com/apk/res/android"
android:layout_width="wrap_conte
nt"
android:layout_height="wrap_cont
ent"
android:stretchColumns="2" >
<TableRow>
<Button
android:layout_width="wr
ap_content"
android:layout_height="w
rap_content"
android:layout_column="0
"
android:text="按钮1" />
<Button
android:layout_width="wr
ap_content"
android:layout_height="w
rap_content"
android:layout_column="1
"
android:text="按钮2" />
</TableRow>
<TableRow>
<Button
android:layout_width="wr
ap_content"
android:layout_height="w
rap_content"
android:layout_column="1
"
android:text="按钮3" />
<Button
android:layout_width="wr
ap_content"
android:layout_height="w
rap_content"
android:layout_column="2
"
android:text="按钮4" />
</TableRow>
</TableLayout>
适用场景
- 数据表格显示
- 登录表单等需要行列对齐的界面
- 网格状布局
4. FrameLayout(帧布局)
特点
- 层叠显示 :子元素默认堆叠在左上角
- 布局简单 :适合简单的层叠效果
- 定位灵活 :通过 android:layout_gravity 控制子元素位置
示例代码
<FrameLayout xmlns:android="http://
schemas.android.com/apk/res/android"
android:layout_width="match_pare
nt"
android:layout_height="match_par
ent">
<Button
android:id="@+id/btn_one"
android:layout_width="300dp"
android:layout_height="300dp
"
android:layout_gravity="cent
er"
android:background="#ff0000"
/>
<Button
android:id="@+id/btn_two"
android:layout_width="220dp"
android:layout_height="220dp
"
android:layout_gravity="cent
er"
android:background="#00ff00"
/>
<Button
android:id="@+id/btn_three"
android:layout_width="140dp"
android:layout_height="140dp
"
android:layout_gravity="cent
er"
android:background="#0000ff"
/>
</FrameLayout>
适用场景
- 层叠效果(如霓虹灯效果)
- 简单的覆盖显示
- 作为容器嵌套其他布局
布局选择建议
- LinearLayout :适合简单的线性排列,如列表、表单等
- RelativeLayout :适合复杂的相对位置布局,如大多数应用的主界面
- TableLayout :适合表格形式的数据展示,如登录表单
- FrameLayout :适合层叠效果,如霓虹灯、图片叠加等