它的子控件的屬性是有規律可循的
一、居中屬性
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
其中一個示例代碼:
<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"
tools:context=".groupview.RelativeLayoutActivity">
<TextView
android:id="@+id/btn_system_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="TextView1"
android:background="@drawable/btn_bg"
android:textColor="@color/white"
android:layout_marginTop="40dp"
/>
</RelativeLayout>
二、相對父控件RelativeLayout對齊
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
示例代碼:
<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"
tools:context=".groupview.RelativeLayoutActivity">
<TextView
android:id="@+id/btn_system_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:text="TextView1"
android:background="@drawable/btn_bg"
android:textColor="@color/white"
android:textSize="32sp"
/>
</RelativeLayout>
效果圖:
三、相對兄弟控件的對齊和定位
先說定位:
如:android:layout_toRightOf:我在誰的右邊
示例代碼:
<?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"
tools:context=".groupview.RelativeLayoutActivity">
<TextView
android:id="@+id/tv_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView1"
android:background="@drawable/btn_bg"
android:textColor="@color/white"
android:textSize="32sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/tv_1"
android:text="TextView2"
android:background="@drawable/btn_bg"
android:textColor="@color/white"
android:textSize="32sp"/>
</RelativeLayout>
效果圖:
對齊:
android:layout_alignLeft="@id/tv_1" //和tv_1左對齊
android:layout_below="@id/tv_1" //在tv_1下面
示例代碼:
<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"
tools:context=".groupview.RelativeLayoutActivity">
<TextView
android:id="@+id/tv_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView1"
android:background="@drawable/btn_bg"
android:textColor="@color/white"
android:textSize="32sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/tv_1"
android:layout_below="@id/tv_1"
android:text="TextView2"
android:background="@drawable/btn_bg"
android:textColor="@color/white"
android:textSize="32sp"/>
</RelativeLayout>
效果圖:
基本弄懂一個其他的就懂了