前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >相对布局RelativeLayout(三)

相对布局RelativeLayout(三)

作者头像
李小白是一只喵
发布2020-04-24 08:46:21
8900
发布2020-04-24 08:46:21
举报

image.png

目录

什么是相对布局

相对布局是通过相对定位的方式让控件出现在布局任意位置.

常见属性

相对于父元素控件布局

属性

含义

android:layout_centerHrizontal

水平居中

android:layout_centerVertical

垂直居中

android:layout_centerInparent

相对于父元素完全居中

android:layout_alignParentBottom

位于父元素的下边缘

android:layout_alignParentLeft

位于父元素的左边缘

android:layout_alignParentRight

位于父元素的右边缘

android:layout_alignParentTop

位于父元素的上边缘

android:layout_alignWithParentIfMissing

如果对应的兄弟元素找不到的话就以父元素做参照物

相对于某个元素控件布局

注意:属性值必须为id的引用名“@id/id-name”

属性

含义

android:layout_below

位于元素的下方

android:layout_above

位于元素的的上方

android:layout_toLeftOf

位于元素的左边

android:layout_toRightOf

位于元素的右边

android:layout_alignTop

该元素的上边缘和某元素的的上边缘对齐

android:layout_alignLeft

该元素的左边缘和某元素的的左边缘对齐

android:layout_alignBottom

该元素的下边缘和某元素的的下边缘对齐

android:layout_alignRight

该元素的右边缘和某元素的的右边缘对齐

相对像素值

属性

含义

android:layout_marginBottom

底边缘的距离

android:layout_marginLeft

左边缘的距离

android:layout_marginRight

右边缘的距离

android:layout_marginTop

上边缘的距离

实战

相对于父元素控件布局

使用相对水平和相对垂直实现控件居中:

android:layout_centerHorizontal="true" android:layout_centerVertical="true"

全部配置:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="368dp"
        android:layout_height="495dp"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp">

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="相对布局" />
    </RelativeLayout>
</android.support.constraint.ConstraintLayout>

执行程序:

image.png

相对于某个元素控件布局

使用和某元素的的左边缘对齐 :

android:layout_alignLeft="@id/textView"

全部配置:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <RelativeLayout
        android:layout_width="368dp"
        android:layout_height="495dp"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp">

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="相对布局" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@id/textView"

        android:text="test1" />

    </RelativeLayout>
</android.support.constraint.ConstraintLayout>

效果图:

image.png

执行程序:

image.png

相对像素值

使用左边缘距离和上边缘距离:

android:layout_marginLeft="20dp"
android:layout_marginTop="200dp"

全部配置:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="368dp"
        android:layout_height="495dp"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp">

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="200dp"
            android:text="相对布局" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@id/textView"

        android:text="test1" />

    </RelativeLayout>
</android.support.constraint.ConstraintLayout>

效果图:

image.png

执行程序:

image.png

参考

最新Android开发视频教程(共6章)Android Studio教程(2017-2018) 19 Android 相对布局的使用(视频+笔记,从01开始点点入门) Android studio 相对布局常见属性 Android----------线性布局和相对布局的使用

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 目录
  • 什么是相对布局
  • 常见属性
    • 相对于父元素控件布局
      • 相对于某个元素控件布局
      • 实战
        • 相对于父元素控件布局
          • 相对于某个元素控件布局
          • 参考
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档