当前位置: 首页 > 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.dtcms.com/a/316147.html

相关文章:

  • 2023 年 6 月 GESP Python 二级试卷真题+答案+解析
  • 基于 Lyapunov 能量函数的等势面绘制方法 —— MATLAB 实现与工程应用拓展
  • 永磁同步电机的矢量控制
  • CPP引用
  • 组织架构与软件架构协同演进实践指南
  • UE5 安装Visual Studio
  • Go语言实战案例:使用context控制协程取消
  • GB28181监控平台LiveGBS如何配置GB28181对接海康、大华解码器上墙,将GB28181平台是视频给硬件解码器解码上墙
  • 软件无线电 招标参数
  • ⭐CVPR2025 非均匀运动视频插帧新突破
  • 文献阅读 | Briefings in Bioinformatics | Hiplot:全面且易于使用的生物医学可视化分析平台
  • HarmonyOS 应用拉起系列(二):如何拉起微信小程序
  • 前端1.0
  • 查看 Redis 某个数据库的内存占用
  • python+MySQL组合实现生成销售财务报告
  • 站在前端的角度,看鸿蒙页面布局
  • MTK-Android 系统拷贝预置资源
  • 本地使用uv管理的python项目怎么部署到服务器?
  • Next.js 链接与导航:页面间无缝切换
  • 最新安卓原生对接苹果cms App后端+app(最新优化版)
  • Spring Cloud系列—简介
  • 从循环嵌套到拓扑编排:LangGraph如何重构Agent工作流
  • 网络 —— 笔记本(主机)、主机虚拟机(Windows、Ubuntu)、手机(笔记本热点),三者进行相互ping通
  • 企业AI转型之战:Coze、Dify与FastGPT的巅峰对决
  • css动态样式
  • Linux 内存管理之 Rmap 反向映射(二)
  • 去哪儿StarRocks实践
  • 以Linux为例补充内存管理基础知识
  • 【 IPMI 内核模块】重新加载
  • BeeWorks私有化即时通讯,局域网办公安全可控