安卓布局 - 相对布局(RelativeLayout)


<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 控件,字体大小和颜色不同。
安卓布局 - 相对布局(RelativeLayout)_第1张图片
上图是它们在模拟器上的效果,我们发现2个控件重叠在一起了,后面的覆盖在了最上面。2个控件默认是顶部对齐和左对齐。

对齐

layout_alignParentRight="true" #对齐父控件(右边)
layout_toRightOf #在某个控件的右边

我们用4个TextView 来演示:


<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 的右边,tv3tv2 的右边,tv1 在其父控件的右边。
效果如下图:
安卓布局 - 相对布局(RelativeLayout)_第2张图片

2.构建如图布局
安卓布局 - 相对布局(RelativeLayout)_第3张图片

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

    

你可能感兴趣的:(Android开发基础)