Android四大常用布局详解与实战

在Android开发中,布局资源决定了应用界面的结构与样式。掌握常用的布局类型,是构建美观、适配性强应用的基础。本文通过四个完整项目,详细介绍 线性布局(LinearLayout)帧布局(FrameLayout)相对布局(RelativeLayout)表格布局(TableLayout) 的使用方法与典型场景。

一、线性布局(LinearLayout)

线性布局将子视图按水平或垂直方向依次排列,是最基础、使用最广泛的布局之一。

  • 主界面:三个水平排列、权重不同的按钮

🧩 核心代码:

xml 复制代码
<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>

📱 运行结果

三个按钮按权重比 1:1:2 水平排列,按钮3占用更多空间。

💡 关键点

  • orientation 决定方向(horizontal / vertical)
  • layout_weight 按比例分配剩余空间,需配合 layout_width="0dp" 使用

二、帧布局(FrameLayout)

帧布局将子视图堆叠在左上角,后添加的视图会覆盖之前的视图。常用于实现层叠效果或单视图容器。

  • 主界面:五个从大到小、颜色不同的按钮,居中堆叠,模拟"霓虹灯"效果

🧩 核心代码:

xml 复制代码
<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>

📱 运行结果

五个按钮居中叠加,形成从大到小的同心圆效果。

💡 关键点

  • layout_gravity 控制子视图在布局内的对齐方式
  • 后添加的视图会覆盖在前者之上,适合做浮层或装饰

三、相对布局(RelativeLayout)

相对布局通过定义视图之间的相对位置(如左对齐、位于某视图下方)来灵活排列控件。

  • 主界面:三个按钮分别位于底部、水平居中、右侧对齐等位置

🧩 核心代码:

xml 复制代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp">

    <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:text="按钮3"
        android:layout_alignBottom="@id/btn_two"
        android:layout_toRightOf="@id/btn_two"
        android:layout_marginLeft="-5dp"
        android:layout_marginBottom="151dp"/>
</RelativeLayout>

📱 运行结果

按钮1贴底部,按钮2水平居中,按钮3位于按钮2右侧并底部对齐,形成特殊排列。

💡 关键点

  • 常用属性:layout_alignParentBottomlayout_centerHorizontallayout_toRightOf
  • 适合复杂界面,减少布局嵌套,提升性能

四、表格布局(TableLayout)

表格布局以行(TableRow)为单位组织视图,类似HTML的<table>,适合表单类或规则网格界面。

  • 主界面:五行两列式按钮布局

🧩 核心代码:

xml 复制代码
<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_column="0" android:text="按钮1"/>
        <Button android:layout_column="1" android:text="按钮2"/>
    </TableRow>

    <TableRow>
        <Button android:layout_column="1" android:text="按钮3"/>
        <Button android:layout_column="2" android:text="按钮4"/>
    </TableRow>

    <TableRow>
        <Button android:layout_column="2" android:text="按钮5"/>
    </TableRow>
</TableLayout>

📱 运行结果

按钮按指定列位置排布,形成错落有致的表格结构。

💡 关键点

  • stretchColumns 指定可拉伸列
  • layout_column 设置按钮所在列索引
  • 支持合并、隐藏单元格,适合数据展示

总结对比

布局类型 特点 适用场景
线性布局 简单顺序排列,支持权重 列表、表单、工具栏
帧布局 单层堆叠,后层覆盖前层 启动页、浮层、图层效果
相对布局 灵活定位,减少嵌套 复杂界面、自适应布局
表格布局 按行列组织,结构规整 设置页、数据网格

以上四个项目完整演示了Android中四种常用布局的核心用法。在实际开发中,应根据界面复杂度、适配要求和性能需要合理选择布局类型,必要时可混合使用,达到最佳效果。

相关推荐
鸟儿不吃草18 小时前
安卓实现左右布局聊天界面
android·开发语言·python
xxjj998a20 小时前
Laravel 1.x:PHP框架的原始魅力
android·php·laravel
formula1000020 小时前
在iOS/安卓上远程连接任何 Agent!Claude、Codex、Copilot、Gemini、OpenCode 等
android·copilot
该用户可能存在20 小时前
Blbl-android 更新至 v0.1.24,体验更流畅、更稳定
android·哔哩哔哩·电视app·androidtv·bbll·blbl·bilibilitv
lKWO OMET20 小时前
mysql之字符串函数
android·数据库·mysql
liang_jy1 天前
Android SparseArray
android·源码
liang_jy1 天前
Activity 启动流程扩展篇(一)—— startActivityInner 任务决策全解析
android·源码
NPE~1 天前
[App逆向]脱壳实战
android·教程·逆向·android逆向·逆向分析
木易 士心1 天前
别再只会用 drawCircle 了!一文搞懂 Android Canvas 底层机制
android
AtOR CUES1 天前
MySQL——表操作及查询
android·mysql·adb