AndroidStudio-常用布局

一、线性布局LinearLayout

线性布局内部的各视图有两种排列方式:

1.orientation属性值为horizontal时,内部视图在水平方向从左往右排列。

2.orientation属性值为vertical时,内部视图在垂直方向从上往下排列。

如果不指定orientation属性,则LinearLayout默认水平方向排列。

例如:

二、线性布局的权重

线性布局的权重,指的是线性布局的下级视图各自拥有多大比例的宽高。

权重属性名叫layout_weight,但该属性不在LinearLayout节点设置,而在线性布局的直接下级视图设置,表示该下级视图占据的宽高比例。

layout_width填0dp时,layout_weight表示水平方向的宽度比例。

layout_height填0dp时,layout_weight表示垂直方向的高度比例。

例如:

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!--
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="横排第一个"
            android:textSize="17sp"
            android:textColor="#000000"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="横排第二个"
            android:textSize="17sp"
            android:textColor="#000000"
            />
    </LinearLayout>

    <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="竖排第一个"
            android:textSize="17sp"
            android:textColor="#000000"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="竖排第二个"
            android:textSize="17sp"
            android:textColor="#000000"
            />
    </LinearLayout>
    -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="横排第一个"
            android:textSize="17sp"
            android:textColor="#000000"
            />
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="横排第二个"
            android:textSize="17sp"
            android:textColor="#000000"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="竖排第一个"
            android:textSize="17sp"
            android:textColor="#000000"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:text="竖排第二个"
            android:textSize="17sp"
            android:textColor="#000000"
            />
    </LinearLayout>


</LinearLayout>

二、相对布局RelativeLayout

相对布局的下级视图位置由其他视图决定。用于确定下级视图位置的参照物分两种:

1.与该视图自身平级的视图;

2.该视图的上级视图(也就是它归属的RelativeLayout)

如果不设定下级视图的参照物,那么下级视图默认显示在RelativeLayout内部的左上角。

相对位置的取值:

例如:

1.layout_centerInParent

XML 复制代码
<?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="150dp"
    tools:context=".RelativeLayoutActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="#ffffff"
        android:text="我在中间"
        android:textSize="11sp"
        android:textColor="#000000"
        />

</RelativeLayout>

2.layout_centerHorizontal

XML 复制代码
<?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="150dp"
    tools:context=".RelativeLayoutActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="#ffffff"
        android:text="我在中间"
        android:textSize="11sp"
        android:textColor="#000000"
        />

    <TextView
        android:id="@+id/tv_center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="#ffffff"
        android:text="水平中间"
        android:textColor="#000000"
        android:textSize="11sp"
        />

</RelativeLayout>

3.layout_centerVertical

XML 复制代码
<?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="150dp"
    tools:context=".RelativeLayoutActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="#ffffff"
        android:text="我在中间"
        android:textSize="11sp"
        android:textColor="#000000"
        />

    <TextView
        android:id="@+id/tv_center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="#ffffff"
        android:text="水平中间"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:background="#ffffff"
        android:text="垂直中间"
        android:textColor="#000000"
        android:textSize="11sp"
        />

</RelativeLayout>

4.layout_alignParentLeft

XML 复制代码
<?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="150dp"
    tools:context=".RelativeLayoutActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="#ffffff"
        android:text="我在中间"
        android:textSize="11sp"
        android:textColor="#000000"
        />

    <TextView
        android:id="@+id/tv_center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="#ffffff"
        android:text="水平中间"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:background="#ffffff"
        android:text="垂直中间"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_parent_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:background="#ffffff"
        android:text="和上级左边对齐"
        android:textColor="#000000"
        android:textSize="11sp"
        />

</RelativeLayout>

5.layout_alignParentRight

XML 复制代码
<?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="150dp"
    tools:context=".RelativeLayoutActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="#ffffff"
        android:text="我在中间"
        android:textSize="11sp"
        android:textColor="#000000"
        />

    <TextView
        android:id="@+id/tv_center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="#ffffff"
        android:text="水平中间"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:background="#ffffff"
        android:text="垂直中间"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_parent_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:background="#ffffff"
        android:text="和上级左边对齐"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_parent_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="#ffffff"
        android:text="和上级右边对齐"
        android:textColor="#000000"
        android:textSize="11sp"
        />

</RelativeLayout>

6.layout_alignParentTop

XML 复制代码
<?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="150dp"
    tools:context=".RelativeLayoutActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="#ffffff"
        android:text="我在中间"
        android:textSize="11sp"
        android:textColor="#000000"
        />

    <TextView
        android:id="@+id/tv_center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="#ffffff"
        android:text="水平中间"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:background="#ffffff"
        android:text="垂直中间"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_parent_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:background="#ffffff"
        android:text="和上级左边对齐"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_parent_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="#ffffff"
        android:text="和上级右边对齐"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_parent_top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="#ffffff"
        android:text="和上级顶部对齐"
        android:textColor="#000000"
        android:textSize="11sp"
        />

</RelativeLayout>

7.layout_alignParentBottom

XML 复制代码
<?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="150dp"
    tools:context=".RelativeLayoutActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="#ffffff"
        android:text="我在中间"
        android:textSize="11sp"
        android:textColor="#000000"
        />

    <TextView
        android:id="@+id/tv_center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="#ffffff"
        android:text="水平中间"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:background="#ffffff"
        android:text="垂直中间"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_parent_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:background="#ffffff"
        android:text="和上级左边对齐"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_parent_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="#ffffff"
        android:text="和上级右边对齐"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_parent_top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="#ffffff"
        android:text="和上级顶部对齐"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_parent_bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#ffffff"
        android:text="和上级底部对齐"
        android:textColor="#000000"
        android:textSize="11sp"
        />

</RelativeLayout>

8.layout_toLeftOf

XML 复制代码
<?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="150dp"
    tools:context=".RelativeLayoutActivity">

    <TextView
        android:id="@+id/tv_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="#ffffff"
        android:text="我在中间"
        android:textSize="11sp"
        android:textColor="#000000"
        />

    <TextView
        android:id="@+id/tv_center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="#ffffff"
        android:text="水平中间"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:background="#ffffff"
        android:text="垂直中间"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_parent_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:background="#ffffff"
        android:text="和上级左边对齐"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_parent_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="#ffffff"
        android:text="和上级右边对齐"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_parent_top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="#ffffff"
        android:text="和上级顶部对齐"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_parent_bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#ffffff"
        android:text="和上级底部对齐"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_left_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/tv_center"
        android:background="#ffffff"
        android:text="在中间的左边"
        android:textColor="#000000"
        android:textSize="11sp"
        />

</RelativeLayout>

9.layout_toRightOf

XML 复制代码
<?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="150dp"
    tools:context=".RelativeLayoutActivity">

    <TextView
        android:id="@+id/tv_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="#ffffff"
        android:text="我在中间"
        android:textSize="11sp"
        android:textColor="#000000"
        />

    <TextView
        android:id="@+id/tv_center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="#ffffff"
        android:text="水平中间"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:background="#ffffff"
        android:text="垂直中间"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_parent_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:background="#ffffff"
        android:text="和上级左边对齐"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_parent_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="#ffffff"
        android:text="和上级右边对齐"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_parent_top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="#ffffff"
        android:text="和上级顶部对齐"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_parent_bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#ffffff"
        android:text="和上级底部对齐"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_left_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/tv_center"
        android:background="#ffffff"
        android:text="在中间的左边"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_right_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/tv_center"
        android:layout_alignBottom="@+id/tv_center"
        android:background="#ffffff"
        android:text="在中间的右边"
        android:textColor="#000000"
        android:textSize="11sp"
        />

</RelativeLayout>

10.layout_above

XML 复制代码
<?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="150dp"
    tools:context=".RelativeLayoutActivity">

    <TextView
        android:id="@+id/tv_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="#ffffff"
        android:text="我在中间"
        android:textSize="11sp"
        android:textColor="#000000"
        />

    <TextView
        android:id="@+id/tv_center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="#ffffff"
        android:text="水平中间"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:background="#ffffff"
        android:text="垂直中间"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_parent_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:background="#ffffff"
        android:text="和上级左边对齐"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_parent_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="#ffffff"
        android:text="和上级右边对齐"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_parent_top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="#ffffff"
        android:text="和上级顶部对齐"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_parent_bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#ffffff"
        android:text="和上级底部对齐"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_left_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/tv_center"
        android:background="#ffffff"
        android:text="在中间的左边"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_right_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/tv_center"
        android:layout_alignBottom="@+id/tv_center"
        android:background="#ffffff"
        android:text="在中间的右边"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_above_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/tv_center"
        android:layout_alignLeft="@+id/tv_center"
        android:background="#ffffff"
        android:text="在中间的上面"
        android:textColor="#000000"
        android:textSize="11sp"
        />

</RelativeLayout>

11.layout_below

XML 复制代码
<?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="150dp"
    tools:context=".RelativeLayoutActivity">

    <TextView
        android:id="@+id/tv_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="#ffffff"
        android:text="我在中间"
        android:textSize="11sp"
        android:textColor="#000000"
        />

    <TextView
        android:id="@+id/tv_center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="#ffffff"
        android:text="水平中间"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:background="#ffffff"
        android:text="垂直中间"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_parent_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:background="#ffffff"
        android:text="和上级左边对齐"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_parent_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="#ffffff"
        android:text="和上级右边对齐"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_parent_top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="#ffffff"
        android:text="和上级顶部对齐"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_parent_bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#ffffff"
        android:text="和上级底部对齐"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_left_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/tv_center"
        android:background="#ffffff"
        android:text="在中间的左边"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_right_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/tv_center"
        android:layout_alignBottom="@+id/tv_center"
        android:background="#ffffff"
        android:text="在中间的右边"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_above_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/tv_center"
        android:layout_alignLeft="@+id/tv_center"
        android:background="#ffffff"
        android:text="在中间的上面"
        android:textColor="#000000"
        android:textSize="11sp"
        />

    <TextView
        android:id="@+id/tv_below_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_center"
        android:layout_alignRight="@+id/tv_center"
        android:background="#ffffff"
        android:text="在中间的下面"
        android:textColor="#000000"
        android:textSize="11sp"
        />

</RelativeLayout>

三、网格布局GridLayout

网格布局支持多行多列的表格排列。

网格布局默认从左往右、从上到下排列,它新增了两个属性:

1.columnCount属性,它指定了网格的列数,即每行能放多少个视图;

2.rowCount属性,它指定了网格的行数,即每列能放多少个视图;

设置以下视图:

代码如下:

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="2"
    android:rowCount="2">

    <TextView
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:background="#ffcccc"
        android:text="浅红色"
        android:gravity="center"
        android:layout_columnWeight="1"
        android:textColor="#000000"
        android:textSize="17sp"
        />

    <TextView
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:background="#ffaa00"
        android:text="橙色"
        android:layout_columnWeight="1"
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="17sp"
        />

    <TextView
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:background="#00ff00"
        android:text="绿色"
        android:layout_columnWeight="1"
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="17sp"
        />

    <TextView
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:background="#660066"
        android:text="深紫色"
        android:layout_columnWeight="1"
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="17sp"
        />


</GridLayout>
相关推荐
AD钙奶-lalala1 小时前
Android 11以上App主动连接WIFI的完整方案
android
大耳猫1 小时前
Android 中的 DataBinding 详解
android·android jetpack·databinding
玲小珑1 小时前
Auto.js 入门指南(一)什么是 Auto.js
android·前端
Fastcv2 小时前
手把手教你上传安卓库到Central Portal
android·maven·jcenter
whysqwhw2 小时前
安卓应用线程与架构问题
android
小鱼干coc2 小时前
Android 轻松实现 增强版灵活的 滑动式表格视图
android
Le_ee3 小时前
dvwa6——Insecure CAPTCHA
android·安全·网络安全·靶场·dvwa
django-尿素3 小时前
django入门-orm数据库操作
android·数据库·django
封妖九禁苍天泣3 小时前
Glide NoResultEncoderAvailableException异常解决
android
Yusei_05235 小时前
C++ 模版复习
android·java·c++