Android 布局介绍
- [Android 布局介绍](#Android 布局介绍)
Android 布局介绍
1、LinearLayout(线性布局)
LinearLayout(线性布局)是 Android 开发中最基础、最常用的布局管理器之一。它将子视图(Views)按照一个方向(水平或垂直)一个接一个地排列。
-
核心排列方式
通过
android:orientation属性来控制子视图的排列方向:- vertical (垂直):子视图从上到下排成一列。
- horizontal (水平):子视图从左到右排成一排(默认值)。
-
重要属性
- android:layout_weight (权重):这是 LinearLayout 的强大功能,允许子视图按比例瓜分剩余空间。例如,如果你有两个按钮,权重都设为 1,它们将平分整个布局的宽度或高度。
- android:gravity:控制布局内所有子视图的整体对齐方式(如居中、靠左等)。
- android:layout_gravity:控制单个子视图在父布局中的对齐方式。
-
XML 示例代码
这是一个简单的垂直布局,包含两个填满宽度的文本框:
xml
<LinearLayout
xmlns:android="http://schemas.microsoft.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第一个视图"
android:background="#FFCC00" />
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="第二个视图(占据剩余所有空间)"
android:background="#00CCFF" />
</LinearLayout>
优缺点
- 优点:简单直观,非常适合排列简单的列表、工具栏或表单。
- 缺点:在处理复杂的嵌套布局时,性能不如
ConstraintLayout(约束布局),因为过多的嵌套会增加系统计算界面的层级深度(布局层级扁平化是性能优化的重点)。
如果界面逻辑比较简单,LinearLayout是首选;如果是复杂的 UI,建议考虑使用ConstraintLayout。
2、RelativeLayout(相对布局)
RelativeLayout(相对布局)是 Android 中历史非常悠久的一个布局管理器。它的核心逻辑是:通过子视图之间的相对位置关系,或者相对于父容器的位置来决定布局。在 ConstraintLayout 出现之前,它是解决"布局嵌套"问题的主要手段。
-
核心排列方式
它不像 LinearLayout 那样有一个固定的"排队"方向,它的每个子视图都需要显式说明"我该放在谁的旁边/下面"。如果你不指定位置,所有子视图都会默认叠加在左上角。
-
常用属性分类
- 相对于父容器 (Parent) :
android:layout_alignParentTop="true":紧贴父容器顶部。
android:layout_centerInParent="true":在父容器中完全居中。 - 相对于兄弟视图 (Siblings) :
android:layout_below="@id/textView1":放在某个 ID 视图的下方。
android:layout_toEndOf="@id/imageView1":放在某个 ID 视图的右侧。 - 对齐方式方式 :
android:layout_alignBottom="@id/textView1":与另一个视图的底部对齐。
- XML 示例代码
演示一个基本的 RelativeLayout 结构:一个按钮在文本框的下方:
xml
<RelativeLayout
xmlns:android="http://schemas.microsoft.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/titleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标题"
android:layout_centerHorizontal="true" /> <!-- 水平居中 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定"
android:layout_below="@id/titleText" <!-- 放在标题下方 -->
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true" />
</RelativeLayout>
现状与建议
曾经它是为了减少 LinearLayout 嵌套而生的,是过去实现复杂 UI 的首选,现在基本已被 ConstraintLayout 取代。
ConstraintLayout 包含了 RelativeLayout 的所有功能,且性能更好、约束更灵活。
RelativeLayout 在测量视图位置时,通常需要对子视图进行两轮测量(double measure),在视图非常复杂时性能会显著下降。
3、ConstraintLayout(约束布局)
ConstraintLayout(约束布局)是 Android 官方目前最推荐使用的布局容器。它的核心思想是通过定义视图之间的"约束关系"来决定位置,类似于"把视图用橡皮筋连在父容器或其他视图上"。
- 为什么要用它?(核心优势)
- 防止布局嵌套(Flat Hierarchy) :在
LinearLayout或RelativeLayout中,复杂的界面往往需要多层嵌套。而ConstraintLayout可以让你在一个层级内完成几乎所有复杂的逻辑,这极大地提高了界面渲染性能。 - 灵活性极高:它结合了相对布局的灵活性和线性布局的权重分配功能。
- 核心概念
- 相对定位 (Relative Positioning) :你可以将一个视图的左边约束在另一个视图的右边,例如:
app:layout_constraintStart_toEndOf="@+id/button1"。 - 居中与偏差 (Bias) :如果一个视图左右两边都加了约束,它会默认居中。通过
app:layout_constraintHorizontal_bias="0.3",你可以调整它偏左或偏右。 - 链 (Chains):可以将多个视图连在一起形成一个"链条",实现类似 LinearLayout 的权重(Weight)分配。
- 辅助工具 :
- Guideline (参照线):不可见的线,用来辅助定位。
- Barrier (屏障):动态根据多个视图的最大宽度/高度形成的线。
- Group (分组):批量控制多个视图的可见性(Visibility)。
- XML 示例代码
这里演示一个按钮居中,且底部对齐屏幕底部的简单结构:
xml
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.microsoft.com/apk/res/android"
xmlns:app="http://schemas.microsoft.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="约束按钮"
<!-- 水平居中约束:左对齐父布局左边,右对齐父布局右边 -->
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
<!-- 垂直约束:底部对齐父布局底部 -->
app:layout_constraintBottom_toBottomOf="parent"
<!-- 距离底部 32dp -->
android:layout_marginBottom="32dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
总结比较
| 特性 | LinearLayout | RelativeLayout | ConstraintLayout |
|---|---|---|---|
| 排列方式 | 单一方向(行列) | 相对依赖 | 复杂约束关系 |
| 性能 | 高(简单时)/低(嵌套多时) | 中等 | 最高(适合复杂界面) |
| 层级结构 | 容易很深 | 较浅 | 极其扁平 |
| 学习曲线 | 很简单 | 简单 | 略复杂(建议配合控制台可视化拖拽) |
开发建议:简单的水平/垂直排列继续用 LinearLayout。任何复杂、包含多个组件的界面,一律首选 ConstraintLayout。