2.2.2相对布局

2017-10-24  本文已影响0人  EDU_MJ

1 相对布局的概念

相对布局是通过相对定位的方式制定控件位置,即以其他控件或父容器为参照物摆放控件位置,在设计相对布局时,要遵循控件之间的依赖关系。

相对布局以一对<RelativeLayout></RelativeLayout>标签标识。
每个控件都可以有一个id属性,用来标识自己的身份。
例如:为一个Button控件添加id
<Button id="@+id/btn_name/>

2 基本属性


1 相对于父组件的位置,值为true 或 false

属性 含义
android:layout_alignParentTop 将该控件的顶部与其父控件的顶部对齐;
android:layout_alignParentBottom 将该控件的底部与其父控件的底部对齐;
android:layout_alignParentLeft 将该控件的左部与其父控件的左部对齐;
android:layout_alignParentRight 将该控件的右部与其父控件的右部对齐;
android:layout_centerHorizontal 将该控件置于水平居中;
android:layout_centerVertical 将该控件置于垂直居中;
android:layout_centerInParent 将该控件置于父控件的中央;

例如:设置按钮id为btn_center的控件处于父控件的中央

<?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/btn_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="center"
        android:layout_centerInParent="true"/>
    
</RelativeLayout>

2 相对于给定ID控件

属性 含义
android:layout_above 将该控件的底部置于给定ID的控件之上;
android:layout_below 将该控件的底部置于给定ID的控件之下;
android:layout_toLeftOf 将该控件的右边缘与给定ID的控件左边缘对齐;
android:layout_toRightOf 将该控件的左边缘与给定ID的控件右边缘对齐;
android:layout_alignBaseline 将该控件的baseline与给定ID的baseline对齐;
android:layout_alignTop 将该控件的顶部边缘与给定ID的顶部边缘对齐;
android:layout_alignBottom 将该控件的底部边缘与给定ID的底部边缘对齐;
android:layout_alignLeft 将该控件的左边缘与给定ID的左边缘对齐;
android:layout_alignRight 将该控件的右边缘与给定ID的右边缘对齐;

例如:设定id为btn_right的按钮处于btn_center按钮的右侧,且与btn_center的基线对齐

<?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/btn_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="center"
        android:layout_centerInParent="true"/>
    <Button
        android:id="@+id/btn_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="right"
        android:layout_toRightOf="@id/btn_center"
        android:layout_alignBaseline="@id/btn_center"
        />
</RelativeLayout>

3 相对控件之间的间距属性

属性 含义
android:layout_marginTop 上偏移的值;
android:layout_marginBottom 下偏移的值;
android:layout_marginLeft 左偏移的值;
android:layout_marginRight 右偏移的值;

例如:设定btn_right按钮的左侧边距为20dp

<?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/btn_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="center"
        android:layout_centerInParent="true"/>
    <Button
        android:id="@+id/btn_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="right"
        android:layout_toRightOf="@id/btn_center"
        android:layout_alignBaseline="@id/btn_center"
        android:layout_marginLeft="20dp"
        />
</RelativeLayout>

4 设置控件的内部间距

属性 含义
android:paddingTop 设置布局顶部内边距的距离
android:paddingBottom 设置布局顶部内边距的距离
android:paddingLeft 设置布局顶部内边距的距离
android:paddingRight 设置布局顶部内边距的距离
android:paddingpadding 设置布局顶部内边距的距离

例如设置btn_center按钮的顶部内边距为10dp,底部内边距为0dp

<?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/btn_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="center"
        android:layout_centerInParent="true"
        android:paddingTop="10dp"
        android:paddingBottom="0dp"/>
    <Button
        android:id="@+id/btn_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="right"
        android:layout_toRightOf="@id/btn_center"
        android:layout_alignBaseline="@id/btn_center"
        android:layout_marginLeft="20dp"
        />
</RelativeLayout>   
上一篇 下一篇

猜你喜欢

热点阅读