<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一个"
android:textSize="18dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二个"
android:textSize="12dp"
android:textColor="#ff0000"/>
</RelativeLayout>
有2个TextView
控件,字体大小和颜色不同。
上图是它们在模拟器上的效果,我们发现2个控件重叠在一起了,后面的覆盖在了最上面。2个控件默认是顶部对齐和左对齐。
对齐
layout_alignParentRight="true" #对齐父控件(右边)
layout_toRightOf #在某个控件的右边
我们用4个TextView
来演示:
<?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">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一个"
android:textSize="18dp"
android:layout_alignParentRight="true"/>
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二个"
android:textSize="12dp"
android:textColor="#ff0000"/>
<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第三个"
android:textSize="18dp"
android:layout_toRightOf="@id/tv2"/>
<TextView
android:id="@+id/tv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第四个"
android:textSize="18dp"
android:layout_toRightOf="@id/tv3"/>
</RelativeLayout>
从代码可以看出:id为tv4
的控件在tv3
的右边,tv3
在 tv2
的右边,tv1
在其父控件的右边。
效果如下图:
2.构建如图布局
<?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">
<Button
android:id="@+id/center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="中间"
android:layout_centerInParent="true"/>
<Button
android:id="@+id/above"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上边"
android:layout_above="@id/center"
android:layout_alignLeft="@id/center"
android:layout_alignRight="@id/center"/>
<Button
android:id="@+id/below"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下边"
android:layout_below="@id/center"
android:layout_alignLeft="@id/center"
android:layout_alignRight="@id/center"/>
<Button
android:id="@+id/left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="左边"
android:layout_alignBottom="@id/center"
android:layout_alignTop="@id/center"
android:layout_toLeftOf="@id/center"
android:layout_alignParentLeft="true"/>
<Button
android:id="@+id/right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="右边"
android:layout_alignBottom="@id/center"
android:layout_alignTop="@id/center"
android:layout_toRightOf="@id/center"
android:layout_alignParentRight="true"/>
<Button
android:id="@+id/aboveAndLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="左上"
android:layout_toLeftOf="@id/center"
android:layout_above="@id/left"
android:layout_alignParentLeft="true"/>
<Button
android:id="@+id/aboveAndRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="右上"
android:layout_toRightOf="@id/center"
android:layout_above="@id/right"
android:layout_alignParentRight="true"/>
<Button
android:id="@+id/belowAndLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="左下"
android:layout_below="@id/center"
android:layout_toLeftOf="@id/center"
android:layout_alignParentLeft="true"
/>
<Button
android:id="@+id/belowAndRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="右下"
android:layout_below="@id/center"
android:layout_toRightOf="@id/center"
android:layout_alignParentRight="true"
/>
</RelativeLayout>