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

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.xdnf.cn/news/17139.html

相关文章:

  • Leetcode 13 java
  • Django中的转发与重定向详解
  • 物联网后端系统架构:从基础到AI驱动的未来 - 第十章:AI促进IOT领域发生革命式发展
  • C# --- 本地缓存失效形成缓存击穿触发限流
  • eclipse类IDE导入现有工程教程
  • 17day-人工智能-机器学习-分类算法-KNN
  • Spring IOC:Java开发中的依赖魔法
  • 【Qt开发】常用控件(一)
  • HTTP性能优化实战:解决高并发场景下的连接瓶颈与延迟问题
  • 【Spring Boot 快速入门】七、阿里云 OSS 文件上传
  • 家庭财务管理系统|基于java和小程序的家庭财务管理系统设计与实现(源码+数据库+文档)
  • 【含文档+PPT+源码】基于SSM的旅游与自然保护平台开发与实现
  • 3D 材质与纹理:让虚拟模型 “以假乱真” 的核心密码
  • 基于ARM+FPGA光栅数据采集卡设计
  • Datart:开源数据可视化的新星,赋能企业数据分析
  • Flink CDC 介绍
  • Linux 系统重置用户密码指南
  • docker镜像源配置教程,以及解决安装好docker配置镜像源后,出现报错。Job for docker.service failed
  • 【Canvas与文字】生存与生活
  • WPF中引用其他元素各种方法
  • 【Mac】OrbStack:桌面端虚拟机配置与使用
  • 机器学习05——正则化与逻辑回归
  • 数据结构:反转链表(reverse the linked list)
  • 华为OD机考2025C卷 - 开源项目热度榜单 (Java Python JS C++ C )
  • Android Espresso 测试框架深度解析:从入门到精通
  • 如何设计和实施高效的向量化数据检索解决方案
  • python基础:数据解析BeatuifulSoup,不需要考虑前端形式的一种获取元素的方法
  • 量子计算接口开发:Python vs Rust 性能对决
  • 我用一个 Postgres 实现一整套后端架构!
  • 分布式版本控制工具Git