1. 对齐至控件的基准线
2. 与父控件的四个边缘对齐
3. 对齐至父控件的中央
1. 对齐至控件的基准线
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context="first.pack.MainActivity$PlaceholderFragment" > 6 7 <TextView 8 android:id="@+id/firstView" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:textSize="50sp" 12 android:background="#FF0000" 13 android:text="hello" /> 14 15 <TextView 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 android:layout_toRightOf="@+id/firstView" 19 android:layout_alignBaseline="@+id/firstView" //对齐基准线!!! 20 android:background="#00FF00" 21 android:text="mirrorhanyu" /> 22 23 </RelativeLayout>
2. 与父控件的四个边缘对齐
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context="first.pack.MainActivity$PlaceholderFragment" > 6 7 <TextView 8 android:id="@+id/firstView" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:textSize="50sp" 12 android:background="#FF0000" 13 android:layout_alignParentRight="true" //与父控件右边对齐 14 android:text="hello" /> 15 16 </RelativeLayout>
当代码为
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context="first.pack.MainActivity$PlaceholderFragment" > 6 7 <RelativeLayout 8 android:layout_width="wrap_content" 9 android:layout_height="300dp" 10 android:background="#00FF00"> 11 <TextView 12 android:id="@+id/firstView" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:textSize="50sp" 16 android:background="#FF0000" 17 android:layout_alignParentRight="true" //与就近的父控件对齐 18 android:layout_alignParentBottom="true" 19 android:text="hello" /> 20 </RelativeLayout> 21 </RelativeLayout>
3. 对齐至父控件的中央