2.2.2相对布局

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>   
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,554评论 0 17
  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,815评论 1 92
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,581评论 25 708
  • 看了几篇文章以及自己写了一些,关于布局的问题,根据别人的写作总结一下得到 一.基本理论Android六大基本布局分...
    shuaikun阅读 837评论 0 4
  • 纤月黄昏院,轻晕人醉浅。 笼花娇欲泣,忆久只自怜。 青丝待霜白,红妆换旧颜。 玉枕泪痕泫,情深无由怨。
    时影书阅读 287评论 0 6