Android基础-相对布局(电影页面为例)

1.相对于父类的相对布局 

   android:layout_centerInParent: 中间位置 

   android:layout_alignParentLeft:左边位置

   android:layout_alignParentRight:右边位置

   android:layout_alignParentTop:顶部

   android:layout_alignParentBottom:底部

   android:layout_centerHorizontal:水平中间 

   android:layout_centerVertical:垂直中间

2.相对于其他子类的相对布局 

   1.在参照物的某边

       android:layout_toLeftOf: 在左边

       android:layout_toRightOf: 在右边

       android:layout_below: 在下边

       android:layout_above: 在上边 

   2.以参照物的某边线对齐

       android:layout_alignRight: 在边界线的右边
       android:layout_alignLeft: 在边界线的左边

       android:layout_alignTop: 在边界线的上面

       android:layout_alignBottom: 在边界线的下面

相对布局中的例子

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <!--     android:layout_centerInParent="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
          android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"-->
    <TextView
        android:id="@+id/center"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:textSize="30sp"
        android:text="屏幕正中"
        android:background="#ff0000"
        android:layout_centerInParent="true"
       />

    <!--
        1.在参照物的某边
        android:layout_toLeftOf
        android:layout_toRighttOf
        android:layout_below
        android:layout_above
        2.和参照物的某边线对齐
        android:layout_alignRight
        android:layout_alignTop
        android:layout_alignBottom
        android:layout_alignLeft
        -->

    <TextView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:textSize="30sp"
        android:text="中偏左上"
        android:background="#00ff00"
        android:layout_above="@id/center"
        android:layout_toLeftOf="@id/center"
        />

    <TextView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:textSize="30sp"
        android:text="中偏右上"
        android:background="#00ff00"
        android:layout_above="@id/center"
        android:layout_toRightOf="@id/center"
        />
    <TextView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:textSize="30sp"
        android:text="中偏左下"
        android:background="#00ff00"
        android:layout_below="@id/center"
        android:layout_toLeftOf="@id/center"
        />
    <TextView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:textSize="30sp"
        android:text="中偏右下"
        android:background="#00ff00"
        android:layout_below="@id/center"
        android:layout_toRightOf="@id/center"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="和中间上边线对齐"
        android:background="#00ff00"
        android:layout_alignRight="@id/center"
        />
</RelativeLayout>

使用线性布局和相对布局构造电影页面

1.使用线性布局构造上下的主界面,内部界面使用relative,这样的好处可以对里面的布局做处理,如这里对TextView 和 ImageView做了布局设计 

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:layout_weight="2"
        android:background="#ff0000">

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:background="#ff0000">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/icon_3d" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:text="复仇者联盟"
                android:layout_alignParentBottom="true"
                android:textSize="36sp"
                android:textColor="#ffffff"
                android:background="#666666"
                />

        </RelativeLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#ffff00">
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/icon_imax2d" />

            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#ffffff">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/icon_3d" />
                
                <!--相对于父布局的下方-->
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:text="捉妖记"
                    android:layout_alignParentBottom="true"
                    android:textSize="16sp"
                    android:textColor="#ffffff"
                    android:background="#666666"
                    />
            </RelativeLayout>

        </LinearLayout>


    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:background="#00ff00"
        android:layout_weight="1">
        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#0000ff">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/icon_4d" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:text="像物像雨又像风"
                android:layout_alignParentBottom="true"
                android:textSize="16sp"
                android:textColor="#ffffff"
                android:background="#666666"
                />
        </RelativeLayout>


        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#00ffff">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/icon_imax3d" />

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#00ff00">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/icon_4d" />

        </RelativeLayout>

    </LinearLayout>

</LinearLayout>

原文地址:https://www.cnblogs.com/my-love-is-python/p/14514280.html