美文网首页
2.2.2相对布局

2.2.2相对布局

作者: EDU_MJ | 来源:发表于2017-10-24 23:17 被阅读0次

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>   

相关文章

  • 2.2.2相对布局

    1 相对布局的概念 相对布局是通过相对定位的方式制定控件位置,即以其他控件或父容器为参照物摆放控件位置,在设计相对...

  • 布局 - 相对布局

    RelativeLayout 相对布局 相对布局也是一种非常常用的布局,它可以通过相对定位的方式让控件出现在任何位...

  • 相对布局

    1.兄弟控件第一组属性 layout_above layout_below layout_toRightOf la...

  • android第四课。

    今天学习了两种布局方式。一种是线性布局,另一种是相对布局。 线性布局: 相对布局: 相对布局 图标要选择对齐方式

  • iOS 中利用相对布局和绝对布局,对Table中的文字自适应调整

    demo地址: dealText 相对布局 与 绝对布局 1.相对布局: 2.绝对布局: 本demo中自适应高度的...

  • Android之6大布局

    LineLayout (线性布局) RelativeLayout(相对布局) TableLayout(表格布局) ...

  • 2 布局

    LinearLayout(线性布局) RelativeLayout(相对布局) TableLayout(表格布局)...

  • android基础

    布局基本布局 FrameLayout线性布局 LinearLayout相对布局 RelativeLayout绝对布...

  • Android:控件布局(相对布局)RelativeLayout

    RelativeLayout是相对布局控件:以控件之间相对位置或相对父容器位置进行排列。 相对布局常用属性: 子类...

  • Android学习日记

    Day 10 Title 1:UI布局之相对布局RelativeLayout 相对布局是用于使得程序屏幕更加灵活和...

网友评论

      本文标题:2.2.2相对布局

      本文链接:https://www.haomeiwen.com/subject/amrfpxtx.html