菜鸟-创作你的创作

Android Layout布局_android:layout

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


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

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


一、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(线性布局)

示例:

<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(相对布局)

示例:

<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(帧布局)

示例:

<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(表格布局)

示例:

<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+)


三、布局优化建议

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

四、总结

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

退出移动版