目录

一、LinearLayout(线性布局)

1. 基本属性

2. 子控件的重要属性(layout_ 开头,作用于子控件)

3. 示例:垂直排列 + 权重分配

4. 适用场景

二、RelativeLayout(相对布局)

1. 常用属性(子控件的 layout_ 属性)

2. 示例:相对位置排列

3. 适用场景

三、ConstraintLayout(约束布局)

1. 核心概念:约束(Constraint)

2. 常用属性(子控件的 layout_constraintXXX 属性)

3. 示例:居中 + 相对位置

4. 进阶功能(适合复杂布局)

5. 适用场景

四、FrameLayout(帧布局)

1. 常用属性

2. 示例:图片 + 文字叠加

3. 适用场景

五、ScrollView(滚动布局)

1. 注意事项

2. 示例:长文本滚动

3. 适用场景

总结:布局选择指南


在 Android 开发中,布局(Layout) 是用于定义界面结构的 XML 文件(或代码),决定了控件(如按钮、文本框)的位置、大小和排列方式。对于初学者,掌握以下几种常用布局能快速搭建基础界面:

一、LinearLayout(线性布局)

核心特点:控件按水平(horizontal) 或垂直(vertical) 方向线性排列,是最基础、最常用的布局之一。

1. 基本属性
  • android:orientation:排列方向(必选)

    • vertical:垂直排列(从上到下)
    • horizontal:水平排列(从左到右)
  • android:gravity:控制子控件在当前布局中的对齐方式(如居中、靠左)可选值:center(居中)、left(左对齐)、right(右对齐)、top(上对齐)、bottom(下对齐),可组合使用(如 center_vertical|center_horizontal)。

  • android:layout_width / android:layout_height:布局自身的宽高(所有布局和控件都有)

    • match_parent:与父容器同宽 / 高
    • wrap_content:根据内容自适应宽 / 高
    • 具体数值(如 100dp,推荐用 dp 单位适配不同屏幕)。
2. 子控件的重要属性(layout_ 开头,作用于子控件)
  • android:layout_gravity:子控件相对于父布局的对齐方式(仅在 LinearLayout 中有效,与 gravity 区分)。
  • android:layout_weight:权重,用于分配父布局的剩余空间(核心功能)。
3. 示例:垂直排列 + 权重分配
<!-- res/layout/activity_linear.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" <!-- 垂直排列 -->
    android:gravity="center_horizontal"> <!-- 子控件水平居中 -->

    <!-- 按钮1:高度自适应,宽度匹配父布局 -->
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮1"/>

    <!-- 按钮2:占剩余空间的1/3 -->
    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp" <!-- 权重分配时,高度设为0dp -->
        android:layout_weight="1" <!-- 权重1 -->
        android:text="按钮2"/>

    <!-- 按钮3:占剩余空间的2/3 -->
    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2" <!-- 权重2 -->
        android:text="按钮3"/>

</LinearLayout>

效果:按钮 1 在顶部(高度自适应),按钮 2 和按钮 3 平分剩余高度(比例 1:2)。

4. 适用场景
  • 简单的线性排列(如垂直的登录表单、水平的导航栏)。
  • 需要按比例分配空间的场景(如底部按钮栏平分宽度)。

二、RelativeLayout(相对布局)

核心特点:控件通过相对位置(相对于父布局或其他控件)定位,灵活性高,适合复杂布局。

1. 常用属性(子控件的 layout_ 属性)
  • 相对于父布局

    • layout_alignParentTop="true":与父布局顶部对齐
    • layout_alignParentBottom="true":与父布局底部对齐
    • layout_alignParentLeft="true":与父布局左对齐
    • layout_alignParentRight="true":与父布局右对齐
    • layout_centerInParent="true":在父布局中心
  • 相对于其他控件(需先给目标控件设置 id):

    • layout_above="@id/btn1":在 btn1 上方
    • layout_below="@id/btn1":在 btn1 下方
    • layout_toLeftOf="@id/btn1":在 btn1 左侧
    • layout_toRightOf="@id/btn1":在 btn1 右侧
    • layout_alignTop="@id/btn1":与 btn1 顶部对齐
2. 示例:相对位置排列
<!-- res/layout/activity_relative.xml -->
<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" <!-- 定义id,供其他控件参考 -->
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="中心按钮"/>

    <!-- 在中心按钮上方 -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/btn_center" <!-- 相对于中心按钮 -->
        android:layout_alignLeft="@id/btn_center" <!-- 与中心按钮左对齐 -->
        android:text="上方按钮"/>

    <!-- 在中心按钮右侧 -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/btn_center" <!-- 相对于中心按钮 -->
        android:layout_alignTop="@id/btn_center" <!-- 与中心按钮顶部对齐 -->
        android:text="右侧按钮"/>

</RelativeLayout>

效果:中心按钮在屏幕中间,上方按钮在其正上方,右侧按钮在其正右方。

3. 适用场景
  • 控件位置有明确相对关系的场景(如 “确定” 按钮在 “取消” 按钮右侧)。
  • 复杂布局(避免 LinearLayout 过度嵌套导致性能问题)。

三、ConstraintLayout(约束布局)

核心特点:Android 官方推荐的布局(Android Studio 默认布局),通过约束关系定位控件,功能强大,可替代 LinearLayout 和 RelativeLayout,且能减少布局嵌套,提升性能。

1. 核心概念:约束(Constraint)

每个控件需要设置水平约束(左 / 右)和垂直约束(上 / 下),否则会默认显示在左上角。约束可以是相对于父布局、其他控件、辅助线(Guideline)等。

2. 常用属性(子控件的 layout_constraintXXX 属性)
  • 水平约束

    • layout_constraintLeft_toLeftOf:左边缘与目标左边缘对齐(如 parent 表示父布局)
    • layout_constraintLeft_toRightOf:左边缘与目标右边缘对齐
    • layout_constraintRight_toLeftOf:右边缘与目标左边缘对齐
    • layout_constraintRight_toRightOf:右边缘与目标右边缘对齐
  • 垂直约束

    • layout_constraintTop_toTopOf:上边缘与目标上边缘对齐
    • layout_constraintTop_toBottomOf:上边缘与目标下边缘对齐
    • layout_constraintBottom_toTopOf:下边缘与目标上边缘对齐
    • layout_constraintBottom_toBottomOf:下边缘与目标下边缘对齐
  • 居中对齐

    • 水平居中:layout_constraintLeft_toLeftOf="parent" + layout_constraintRight_toRightOf="parent"
    • 垂直居中:layout_constraintTop_toTopOf="parent" + layout_constraintBottom_toBottomOf="parent"
3. 示例:居中 + 相对位置
<!-- res/layout/activity_constraint.xml -->
<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_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="中心按钮"
        <!-- 水平居中:左右约束父布局 -->
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        <!-- 垂直居中:上下约束父布局 -->
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <!-- 在中心按钮上方 -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="上方按钮"
        <!-- 水平与中心按钮对齐 -->
        app:layout_constraintLeft_toLeftOf="@id/btn_center"
        app:layout_constraintRight_toRightOf="@id/btn_center"
        <!-- 垂直约束:上边缘与中心按钮下边缘对齐(实际是上方,需调整) -->
        app:layout_constraintBottom_toTopOf="@id/btn_center"/>

</androidx.constraintlayout.widget.ConstraintLayout>

效果:与 RelativeLayout 的示例类似,但通过约束实现,更灵活。

4. 进阶功能(适合复杂布局)
  • Chain(链):多个控件水平 / 垂直连成链,可设置均匀分布、权重等(类似 LinearLayout 的 weight)。
  • Guideline(辅助线):不可见的参考线,用于对齐控件(如 “距离顶部 20% 位置”)。
  • 百分比布局:通过 layout_constraintWidth_percent 等属性设置控件宽高为父布局的百分比。
5. 适用场景
  • 几乎所有场景(官方推荐),尤其是复杂布局(减少嵌套)和响应式布局(适配不同屏幕)。

四、FrameLayout(帧布局)

核心特点:控件默认堆叠显示(都从左上角开始),后添加的控件会覆盖先添加的,适合简单的叠加场景。

1. 常用属性
  • 子控件通过 layout_gravity 控制在父布局中的位置(如居中、右下角)。
2. 示例:图片 + 文字叠加
<!-- res/layout/activity_frame.xml -->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- 背景图片(占满屏幕) -->
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/bg"
        android:scaleType="centerCrop"/>

    <!-- 文字(居中显示) -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" <!-- 居中 -->
        android:text="Hello FrameLayout"
        android:textColor="@color/white"
        android:textSize="24sp"/>

</FrameLayout>

效果:文字叠加在背景图片的中心。

3. 适用场景
  • 控件叠加显示(如图片上放文字、进度条覆盖在内容上)。
  • 作为 Fragment 的容器(动态切换 Fragment 时,新 Fragment 覆盖旧的)。

五、ScrollView(滚动布局)

核心特点:当内容超过屏幕时,允许垂直滚动(水平滚动用 HorizontalScrollView),避免内容被截断。

1. 注意事项
  • 只能有一个直接子元素(通常是 LinearLayout 或 ConstraintLayout,再在其中放多个控件)。
2. 示例:长文本滚动
<!-- res/layout/activity_scroll.xml -->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- 唯一直接子元素:垂直线性布局 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="16dp">

        <!-- 多个文本框(内容过长) -->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="很长的文本内容..."
            android:textSize="18sp"/>

        <!-- 更多控件... -->

    </LinearLayout>

</ScrollView>

效果:内容超过屏幕时,可上下滚动查看全部。

3. 适用场景
  • 内容过长的页面(如注册表单、详情页)。

总结:布局选择指南

布局类型 核心特点 适用场景
LinearLayout 线性排列,支持权重 简单垂直 / 水平排列、比例分配空间
RelativeLayout 相对位置定位 控件有明确相对关系的复杂布局
ConstraintLayout 约束关系定位,功能强大 几乎所有场景(推荐),尤其是复杂响应式布局
FrameLayout 控件堆叠显示 叠加效果、Fragment 容器
ScrollView 内容可滚动 内容过长的页面

初学者建议先掌握 LinearLayout 和 ConstraintLayout,其中 ConstraintLayout 是未来主流,建议重点学习。布局的核心是 “清晰、简洁、少嵌套”,避免过度复杂的布局结构影响性能。

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐