Android课程---布局管理器之相对布局(二)

这次示例代码是相对布局中兄弟组件之间,设置按钮的位置,难度:*****,一定要注意有同方向和反方向之分:

1.同方向

1)layout_alignLeft 同方向左对齐

2)layout_alignRight 同方向右对齐

3)layout_alignTop 同方向顶部对齐

4)layout_alignBottom 同方向底部对齐

 

2.反方向

1)layout_above 在相对组件的上边
2)layout_below 在相对组件的下边
3)layout_toRightOf 在相对组件的右边
4) layout_toLeftOf 在相对组件的左边

注意:可以用相对的组件的id来设置

代码示例:

<?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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮"
        android:layout_centerInParent="true"
        android:id="@+id/bt"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮"
        android:layout_alignTop="@id/bt"
        android:layout_toLeftOf="@+id/bt"
        /><!--相对于第一个按钮即兄弟而言直接用"@id/bt"-->

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮"
        android:layout_alignLeft="@id/bt"
        android:layout_above="@+id/bt"/>
        <!--above在..之上,上面的顶边与下面的底边对齐反方向-->

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮"
        android:layout_alignLeft="@id/bt"
        android:layout_below="@+id/bt"/>
    <!--below 在..之下-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮"
        android:layout_toRightOf="@id/bt"
        android:layout_alignBottom="@+id/bt"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮"
        android:layout_toRightOf="@id/bt"
        android:layout_below="@+id/bt"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮"
        android:layout_toRightOf="@id/bt"
        android:layout_above="@+id/bt"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮"
        android:layout_toLeftOf="@id/bt"
        android:layout_below="@+id/bt"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮"
        android:layout_toLeftOf="@id/bt"
        android:layout_above="@+id/bt"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入框"
        android:layout_marginBottom="20dp"
        android:layout_marginTop="10dp"
        android:paddingLeft="10dp"
        android:id="@+id/et"/><!--padding内边距-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="OK"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/et"
        android:id="@+id/ok"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cencel"
        android:layout_below="@+id/et"
        android:layout_toLeftOf="@+id/ok"
        android:layout_marginRight="20dp"
        /><!--margin外边距-->

</RelativeLayout>

效果图:

 

posted @ 2016-03-25 21:00  秦萧不再  阅读(256)  评论(0编辑  收藏  举报