下面给你整理一篇 Android Layout 布局详解指南,从基础属性到常用布局类型,便于初学者理解 android:layout_* 系列属性及布局原理。


Android Layout 布局详解(android:layout 系列属性指南)

在 Android 开发中,布局(Layout)决定了控件(View)在屏幕上的排列方式。理解 android:layout 系列属性是实现 UI 设计的基础。


一、Layout 的基础概念

  • Layout(布局):决定控件如何排列和显示
  • ViewGroup:所有布局的父类
  • View:子控件,例如 Button、TextView
  • android:layout_*:控件在父布局中的位置、大小和排列方式

常用 Layout 属性

属性说明
android:layout_width控件宽度,可用值:match_parent / wrap_content / 固定尺寸(如 200dp)
android:layout_height控件高度,同上
android:layout_margin控件外边距,单位 dp
android:layout_marginTop/Bottom/Left/Right单独设置上下左右边距
android:layout_padding控件内边距(View 内部内容距离边界距离)
android:layout_gravity控件在父布局中的对齐方式(LinearLayout、FrameLayout)
android:gravity控件内部内容的对齐方式(TextView、Button 内文字等)
android:layout_weightLinearLayout 中控件占比权重
android:layout_alignParentTop/Bottom/Left/RightRelativeLayout 中控件相对父布局对齐
android:layout_centerInParentRelativeLayout 中控件居中
android:layout_above/belowRelativeLayout 中控件相对其他控件的位置
android:layout_toLeftOf/toRightOfRelativeLayout 中控件相对其他控件左右排列

二、常用布局类型

1. LinearLayout(线性布局)

  • 特点:控件按垂直(vertical)或水平(horizontal)排列
  • 常用属性
    • android:orientation="vertical|horizontal"
    • android:layout_weight:按比例分配剩余空间

示例:

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

    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="Button 1"/>
    
    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:text="Button 2"/>
</LinearLayout>

Button 1 占 1/3,Button 2 占 2/3 高度


2. RelativeLayout(相对布局)

  • 特点:控件可相对父布局或其他控件定位
  • 常用属性
    • layout_alignParentTop/Bottom/Left/Right
    • layout_centerInParent
    • layout_below/above/toLeftOf/toRightOf

示例:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title"
        android:layout_centerHorizontal="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Next"
        android:layout_below="@id/title"
        android:layout_alignParentRight="true"/>
</RelativeLayout>


3. FrameLayout(帧布局)

  • 特点:控件堆叠显示,常用于覆盖层或简单布局
  • 常用属性
    • android:layout_gravity 控件对齐方式

示例:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="200dp">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/bg"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello"
        android:layout_gravity="center"/>
</FrameLayout>

TextView 居中显示在 ImageView 上


4. TableLayout(表格布局)

  • 特点:将控件按行列排列
  • 常用子控件TableRow

示例:

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TableRow>
        <TextView android:text="Name"/>
        <EditText android:layout_width="wrap_content"/>
    </TableRow>

    <TableRow>
        <TextView android:text="Age"/>
        <EditText android:layout_width="wrap_content"/>
    </TableRow>

</TableLayout>


5. GridLayout(网格布局,API 14+)

  • 特点:控件按行列排列,可自定义行列占比
  • 常用属性
    • android:layout_row
    • android:layout_column
    • android:layout_rowSpan / layout_columnSpan

三、布局优化建议

  1. 尽量少嵌套:过多嵌套会影响性能
  2. 使用 weight 分配空间:LinearLayout 中使用 layout_weight 代替固定尺寸,适配性更好
  3. RelativeLayout 优先对齐而非嵌套
  4. ConstraintLayout(推荐):Android Studio 现在推荐使用 ConstraintLayout,实现更复杂布局而减少层级

四、总结

  • LinearLayout:线性排列,简单明了
  • RelativeLayout:相对定位,灵活
  • FrameLayout:堆叠控件,覆盖显示
  • TableLayout/GridLayout:表格/网格排列
  • 布局优化:少嵌套、使用权重、ConstraintLayout

掌握 android:layout_* 系列属性,是 Android UI 开发的基础。