[Android 从零到一] 常用布局:LinearLayout / RelativeLayout / ConstraintLayout

1. 背景

Android 界面由布局(Layout)来组织控件的位置关系。常用的布局有 LinearLayout(线性布局)、RelativeLayout(相对布局)和 ConstraintLayout(约束布局)。新手常遇到「控件不见了」「布局嵌套太深卡顿」「ConstraintLayout 看不懂」等问题,本篇帮你理清三者。

2. LinearLayout 线性布局

按水平或垂直方向依次排列子控件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="第一行" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="第二行" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮" />
</LinearLayout>

常见属性:

  • orientation:vertical(垂直)或 horizontal(水平)
  • layout_weight:权重,按比例分配剩余空间
  • gravity:子控件对齐方式
  • layout_gravity:自身在父容器中对齐方式

权重示例:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <EditText
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="输入内容" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="搜索" />
</LinearLayout>

3. RelativeLayout 相对布局

通过相对位置关系定位子控件,适合复杂布局。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="居中" />

    <Button
        android:id="@+id/btn_above"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/btn_center"
        android:layout_centerHorizontal="true"
        android:text="在上方" />

    <Button
        android:id="@+id/btn_below"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/btn_center"
        android:layout_centerHorizontal="true"
        android:text="在下方" />
</RelativeLayout>

核心定位属性:

  • layout_above / layout_below:在某个控件上方/下方
  • layout_toLeftOf / layout_toRightOf:在某个控件左侧/右侧
  • layout_alignParentTop / layout_alignParentBottom:对齐父容器顶部/底部
  • layout_centerInParent / layout_centerHorizontal:居中

缺点:嵌套过多时性能差,推荐用 ConstraintLayout 替代。

4. ConstraintLayout 约束布局

Google 官方推荐的性能最优布局,扁平化层次,适合复杂 UI。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns: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="match_parent">

    <Button
        android:id="@+id/btn_a"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="A"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_b"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="B"
        app:layout_constraintStart_toEndOf="@id/btn_a"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_c"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="C"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/btn_a"
        app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

核心概念:

  • 每个控件至少需要两个方向(水平+垂直)的约束
  • layout_constraintStart_toEndOf:A 的右侧紧贴 B 的左侧
  • layout_constraintTop_toBottomOf:A 的底部紧贴 B 的顶部
  • layout_constraintHorizontal_bias:水平偏移比例(0~1)
  • 配合 0dp(MATCH_CONSTRAINT)实现百分比宽度

5. 三种布局对比

特性LinearLayoutRelativeLayoutConstraintLayout
性能一般(嵌套多)较差(两次测量)最优
上手难度简单中等较难
灵活度低(只能线性排列)
推荐场景简单列表、表单行较少使用复杂页面

6. 避坑指南

  • 性能陷阱:RelativeLayout 嵌套 RelativeLayout = 性能灾难,尽量用 ConstraintLayout
  • wrap_content 慎用:在 LinearLayout 中配合 weight 使用必须设 width/height=0dp
  • ConstraintLayout 写死宽高:用 0dp 配合约束实现自适应,不要写固定 dp
  • tools 预览:用 tools:visibility / tools:text 做 IDE 预览不影响运行

7. 总结

LinearLayout 适合简单线性排列,RelativeLayout 已被淘汰,ConstraintLayout 是 Google 推荐的现代布局方案。新项目直接上 ConstraintLayout,配合 Android Studio 的布局编辑器拖拽即可。需要性能优化的场景用 ConstraintLayout + 扁平层次。

posted @ 2026-06-10 15:41  天总会晴的  阅读(1)  评论(0)    收藏  举报