Android-布局管理-相对布局

简要概述:

    相对布局是指按照组件之间的相对位置来进行布局,如某个组件在另外一个组件的左边、右边、上方、下方。其基本语法格式以下:android

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    属性列表>
</RelativeLayout>

    RelativeLayout支持的经常使用XML属性以下:布局

XML属性 描述
android:gravity 用于设置布局管理器中各子组件的对齐方式
android:ignoreGravity 用于执行某个组件不受gravity属性的影响

    在相对布局管理器中,只有上面介绍的两个属性是不够的,为了更好的控制该布局管理器中各子组件的布局分布,RelativeLayout提供了一个内部类RelativeLayout.LayoutParams,经过该类提供的大量XML,能够很好的控制相对布局管理器中各组件的分布方式,RelativeLayout.LayoutParams支持的XML属性以下:spa

XML属性 描述
android:layout_above 其属性值为其余UI组件的id属性,用于指定该组件位于那个组件的上方
android:layout_below 其属性值为其余UI组件的id属性,用于指定该组件位于那个组件的下方
android:layout_toLeftOf 其属性值为其余UI组件的id属性,用于指定该组件位于那个组件的左方
android:layout_toRightOf 其属性值为其余UI组件的id属性,用于指定该组件位于那个组件的右方
android:layout_alignTop 其属性值为其余UI组件的id属性,用于指定该组件与哪一个组件的上边界对齐
android:layout_alignBottom 其属性值为其余UI组件的id属性,用于指定该组件与哪一个组件的下边界对齐
android:layout_alignLeft 其属性值为其余UI组件的id属性,用于指定该组件与哪一个组件的左边界对齐
android:layout_alignRight 其属性值为其余UI组件的id属性,用于指定该组件与哪一个组件的右边界对齐
android:layout_alignParentTop 属性为boolean值,用于指定该组件是否与布局管理器顶端对齐
android:layout_alignParentBottom 属性为boolean值,用于指定该组件是否与布局管理器底端对齐
android:layout_alignParentLeft 属性为boolean值,用于指定该组件是否与布局管理器左边对齐
android:layout_alignParentRight 属性为boolean值,用于指定该组件是否与布局管理器右边对齐
android:layout_ 属性为boolean值,用于指定该组件是否位于布局管理器水平居中的位置
android:layout_ 属性为boolean值,用于指定该组件是否位于布局管理器垂直居中的位置
android:layout_ 属性为boolean值,用于指定该组件是否位于布局管理器的中央位置

样例代码清单:

<?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"
    android:gravity="center">

    <TextView
        android:id="@+id/relativetext1"
        android:textSize="48px"
        android:text="发现又新版本,您是否如今就安装吗?"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/relativebutton1"
        android:text="如今更新"
        android:layout_below="@id/relativetext1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/relativebutton2"
        android:text="之后再说"
        android:layout_below="@+id/relativetext1"
        android:layout_toRightOf="@id/relativebutton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

样例效果以下:code

相关文章
相关标签/搜索