Android 常见的布局管理器

在这里插入图片描述在这里插入图片描述

1. 线性布局 (LinearLayout)

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/ll_1"
        android:layout_height="200dp"
        android:layout_width="200dp"
        android:orientation="horizontal"
        android:background="@color/purple_200"
        android:paddingTop="40dp"
        android:paddingBottom="40dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:gravity="center">

        <View
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/teal_200"/>

    </LinearLayout>

</LinearLayout>

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/ll_1"
        android:layout_height="200dp"
        android:layout_width="200dp"
        android:orientation="horizontal"
        android:background="@color/purple_200"
        android:paddingTop="40dp"
        android:paddingBottom="40dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:layout_marginBottom="15dp"
        android:gravity="center">

        <View
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/teal_200"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_2"
        android:layout_height="200dp"
        android:layout_width="200dp"
        android:orientation="horizontal"
        android:background="@color/purple_200"
        android:paddingTop="40dp"
        android:paddingBottom="40dp"
        android:gravity="center">

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/purple_700"/>

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/teal_700"/>

    </LinearLayout>

</LinearLayout>

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/ll_1"
        android:layout_height="200dp"
        android:layout_width="200dp"
        android:orientation="horizontal"
        android:background="@color/purple_200"
        android:paddingTop="40dp"
        android:paddingBottom="40dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:layout_marginBottom="15dp"
        android:gravity="center">

        <View
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/teal_200"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_2"
        android:layout_height="200dp"
        android:layout_width="200dp"
        android:orientation="horizontal"
        android:background="@color/purple_200"
        android:paddingTop="40dp"
        android:paddingBottom="40dp"
        android:gravity="center">

        <View
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/purple_700"/>

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@color/teal_700"/>

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:background="@color/teal_200"/>

    </LinearLayout>

</LinearLayout>

2. 相对布局 (RelativeLayout)

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp"
    tools:context=".MainActivity">

    <View
        android:id="@+id/v_1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/black"
        android:layout_alignParentBottom="true"/>

    <View
        android:id="@+id/v_2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/purple_200"
        android:layout_alignParentEnd="true"/>

    <View
        android:id="@+id/v_3"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/purple_700"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"/>

</RelativeLayout>

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp"
    tools:context=".MainActivity">

    <View
        android:id="@+id/v_1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/black"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"/>

    <View
        android:id="@+id/v_2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/purple_200"
        android:layout_toEndOf="@id/v_1"
        android:layout_centerVertical="true"/>

    <View
        android:id="@+id/v_3"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/teal_200"
        android:layout_below="@id/v_1"
        android:layout_centerHorizontal="true"/>

    <View
        android:id="@+id/v_4"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/teal_700"
        android:layout_above="@id/v_1"
        android:layout_centerHorizontal="true"/>

    <View
        android:id="@+id/v_5"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/purple_500"
        android:layout_toStartOf="@id/v_1"
        android:layout_centerVertical="true"/>

</RelativeLayout>

3. 表格布局 (TableLayout)

在这里插入图片描述

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp"
    android:stretchColumns="1"
    tools:context=".MainActivity">

    <TableRow android:layout_marginBottom="15dp">
        <Button
            android:id="@+id/button_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            tools:ignore="ButtonStyle" />
        <Button
            android:id="@+id/button_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            tools:ignore="ButtonStyle" />
        <Button
            android:id="@+id/button_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            tools:ignore="ButtonStyle" />
    </TableRow>

    <TableRow>
        <Button
            android:id="@+id/button_4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            android:layout_column="1"
            tools:ignore="ButtonStyle" />
    </TableRow>

    <TableRow>
        <Button
            android:id="@+id/button_5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            android:layout_span="2"
            tools:ignore="ButtonStyle" />
    </TableRow>

</TableLayout>

猜你喜欢

转载自blog.csdn.net/YKenan/article/details/112561164