1. 线性布局(LinearLayout)

线性布局是Android开发中最常用的布局之一。它按照水平或垂直方向排列子视图。以下是一个示例:

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

    <TextView
        android:layout_width=\"wrap_content\"
        android:layout_height=\"wrap_content\"
        android:text=\"Hello\" />

    <Button
        android:layout_width=\"wrap_content\"
        android:layout_height=\"wrap_content\"
        android:text=\"Click Me\" />

</LinearLayout>

在这个示例中,我们创建了一个垂直方向的线性布局,其中包含一个TextView和一个Button。

2. 相对布局(RelativeLayout)

相对布局允许我们根据其他视图的位置来定位子视图。以下是一个示例:

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

    <Button
        android:id=\"@+id/button1\"
        android:layout_width=\"wrap_content\"
        android:layout_height=\"wrap_content\"
        android:text=\"Button 1\" />

    <Button
        android:id=\"@+id/button2\"
        android:layout_width=\"wrap_content\"
        android:layout_height=\"wrap_content\"
        android:text=\"Button 2\"
        android:layout_below=\"@id/button1\" />

</RelativeLayout>

在这个示例中,我们创建了一个相对布局,其中包含两个按钮。第二个按钮被设置为位于第一个按钮的下方。

3. 帧布局(FrameLayout)

帧布局允许我们在同一个位置上叠加多个子视图。以下是一个示例:

<FrameLayout
    android:layout_width=\"match_parent\"
    android:layout_height=\"match_parent\">

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

    <TextView
        android:layout_width=\"wrap_content\"
        android:layout_height=\"wrap_content\"
        android:text=\"Overlay Text\"
        android:layout_gravity=\"center\" />

</FrameLayout>

在这个示例中,我们创建了一个帧布局,其中包含一个ImageView和一个TextView。TextView被设置为位于ImageView的中心位置。

4. 网格布局(GridLayout)

网格布局将子视图排列在一个网格中,每个子视图占据一个或多个网格单元。以下是一个示例:

<GridLayout
    android:layout_width=\"match_parent\"
    android:layout_height=\"wrap_content\"
    android:columnCount=\"2\">

    <Button
        android:layout_width=\"wrap_content\"
        android:layout_height=\"wrap_content\"
        android:text=\"Button 1\" />

    <Button
        android:layout_width=\"wrap_content\"
        android:layout_height=\"wrap_content\"
        android:text=\"Button 2\" />

    <Button
        android:layout_width=\"wrap_content\"
        android:layout_height=\"wrap_content\"
        android:text=\"Button 3\" />

    <Button
        android:layout_width=\"wrap_content\"
        android:layout_height=\"wrap_content\"
        android:text=\"Button 4\" />

</GridLayout>

在这个示例中,我们创建了一个网格布局,其中包含四个按钮,每行两个。

5. 约束布局(ConstraintLayout)

约束布局是一种灵活的布局,可以根据视图之间的约束关系来定位和调整子视图。以下是一个示例:

<ConstraintLayout
    android:layout_width=\"match_parent\"
    android:layout_height=\"match_parent\">

    <Button
        android:id=\"@+id/button1\"
        android:layout_width=\"wrap_content\"
        android:layout_height=\"wrap_content\"
        android:text=\"Button 1\"
        app:layout_constraintTop_toTopOf=\"parent\"
        app:layout_constraintStart_toStartOf=\"parent\" />

    <Button
        android:id=\"@+id/button2\"
        android:layout_width=\"wrap_content\"
        android:layout_height=\"wrap_content\"
        android:text=\"Button 2\"
        app:layout_constraintTop_toBottomOf=\"@id/button1\"
        app:layout_constraintStart_toEndOf=\"@id/button1\" />

</ConstraintLayout>

在这个示例中,我们创建了一个约束布局,其中包含两个按钮。第二个按钮被设置为位于第一个按钮的下方,并且相对于第一个按钮的右侧。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注