当前位置: 首页 > news >正文

Android 中几种常用布局的优缺点

1. LinearLayout(线性布局)

示例:垂直排列的登录界面,包含用户名输入框、密码输入框和登录按钮。

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:padding="16dp"><EditTextandroid:hint="用户名"android:layout_width="match_parent"android:layout_height="wrap_content"/><EditTextandroid:hint="密码"android:inputType="textPassword"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"/><Buttonandroid:text="登录"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"/>
</LinearLayout>

优点

  • 结构简单,易于理解和使用,适合线性排列的场景。
  • 性能较好,测量和绘制过程高效。
  • 可通过 layout_weight 实现子视图对剩余空间的比例分配(如底部按钮平分宽度)。

!!!缺点!!!

  • 布局灵活性低,只能水平或垂直单方向排列。
  • 复杂布局需要多层嵌套(如垂直布局中嵌套水平布局),可能导致性能问题(过度绘制)。
  • 无法实现子视图之间的复杂相对关系(如"A 在 B 的右侧且下方")。

2. RelativeLayout(相对布局)

示例:一个包含标题、图标和描述的信息卡片,图标在标题左侧,描述在标题下方。

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="100dp"android:padding="16dp"><ImageViewandroid:id="@+id/icon"android:layout_width="40dp"android:layout_height="40dp"android:src="@mipmap/ic_launcher"android:layout_alignParentLeft="true"android:layout_centerVertical="true"/><TextViewandroid:id="@+id/title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="标题"android:textSize="18sp"android:layout_toRightOf="@id/icon"android:layout_marginLeft="10dp"android:layout_alignTop="@id/icon"/><TextViewandroid:id="@+id/desc"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="这是描述信息"android:textSize="14sp"android:layout_below="@id/title"android:layout_alignLeft="@id/title"/>
</RelativeLayout>

优点

  • 灵活性高,子视图可通过相对关系(如位置、对齐方式)精确定位。
  • 相比多层嵌套的 LinearLayout,能减少布局层级,优化性能。

缺点

  • 布局规则较复杂,子视图关系过多时难以维护。
  • 测量过程可能需要两次遍历(先测量子视图,再根据相对关系调整),复杂布局性能不如 ConstraintLayout。
  • 功能不如 ConstraintLayout 全面(如不支持比例约束)。

3. ConstraintLayout(约束布局)

示例:一个包含图片、标题和按钮的商品展示区,标题居中,按钮固定在底部,图片按比例缩放。

<androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="300dp"><ImageViewandroid:id="@+id/image"android:layout_width="0dp"android:layout_height="0dp"android:scaleType="centerCrop"android:src="@mipmap/ic_launcher"app:layout_constraintTop_toTopOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintBottom_toTopOf="@id/title"app:layout_constraintDimensionRatio="4:3"/><TextViewandroid:id="@+id/title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="商品名称"app:layout_constraintBottom_toTopOf="@id/buyBtn"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintVertical_bias="0.5"/><Buttonandroid:id="@+id/buyBtn"android:layout_width="match_parent"android:layout_height="40dp"android:text="加入购物车"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

优点

  • 灵活性极强,支持相对定位、比例约束、角度约束、链布局等复杂规则。
  • 可在单一布局中实现复杂界面,彻底避免多层嵌套,性能最优。
  • 与 Android Studio 的可视化编辑器结合紧密,拖放操作即可快速构建布局。
  • 支持百分比偏移和宽高比,适配不同屏幕尺寸更方便。

缺点

  • 入门门槛较高,约束关系复杂时需要仔细调试。
  • 简单布局场景下,配置成本略高于 LinearLayout。

4. FrameLayout(帧布局)

示例:一个带加载中的图片显示界面,加载框覆盖在图片上方。

<FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><ImageViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:scaleType="fitCenter"android:src="@mipmap/ic_launcher"/><ProgressBarandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"/>
</FrameLayout>

优点

  • 结构最简单,适合子视图叠加显示的场景(如覆盖层、加载框)。
  • 性能高效,测量和绘制过程简单。

缺点

  • 定位能力弱,默认所有子视图堆叠在左上角,只能通过 layout_gravity 粗略调整位置。
  • 不适合复杂布局,子视图过多时难以管理层级关系。

总结建议

  • 简单线性排列(如列表项、按钮组)→ 优先用 LinearLayout
  • 复杂相对关系布局(如卡片、信息展示)→ 优先用 ConstraintLayout
  • 子视图叠加场景(如加载框、覆盖层)→ 用 FrameLayout
  • 旧项目维护或简单相对定位 → 可沿用 RelativeLayout(新项目建议用 ConstraintLayout 替代)。

选择布局的核心原则:在满足需求的前提下,尽量减少布局嵌套,优先保证性能和可维护性。

http://www.lryc.cn/news/610903.html

相关文章:

  • 如何在nuxt项目中使用scss
  • 自动驾驶中的传感器技术24——Camera(15)
  • AI智能体的安全困境:防护机制与伦理平衡的艺术
  • PostgreSQL bytea 类型的大小限制
  • fastgpt本地运行起来的 服务配置
  • SELinux加固Linux安全
  • 基于Django的计算机资源爬虫及可视化系统的设计与实现
  • 开源密码恢复实用程序 Hashcat 7.0.0 发布
  • 最新安卓原生对接苹果cms App后端+app(最新优化版)
  • QML开发:QML的第一个程序
  • echarts在前后端分离项目中的实践与应用
  • C# --- 本地缓存失效形成缓存击穿触发限流
  • RHCA04--系统模块管理与资源限制
  • 武汉火影数字:VR大空间在文旅产业的创新应用
  • TDengine 中 TDgpt 的模型评估工具
  • VR眼动追踪技术帮助医生更快速确认大脑神经损伤与疾病
  • 与功能包相关的指令ros2 pkg
  • Reading Books(Sorting and Searching)
  • 工作相关: 预刷真值与人工标注的真值之间的关系 以及 真值与原始数据的关系,
  • Node.js高并发下的内存泄漏排查与解决实录
  • postman接口测试实战
  • 前端性能测试:从工具到实战全解析
  • 奇偶校验码原理与FPGA实现
  • Z20K118库中寄存器及其库函数封装-CLOCK库
  • 通信算法之298: verilog语法generate和for介绍
  • 【学习笔记】FTP库函数学习
  • uniapp云打包打包安卓app失败,显示:本地安装包生成失败,请重试或者切换到非安心打包模式进行打包
  • 多模态新方向|从数据融合到场景落地,解锁视觉感知新范式
  • SOLIDWORKS 买断许可和订阅许可的资金流影响分析-代理商硕迪科技
  • Windows 安装 RabbitMQ 消息队列超详细步骤(附加详细操作截屏)