相对布局(RelativeLayout)

RelativeLayout是一种相对布局,控件的位置是按照相对位置来计算的,后一个控件在什么位置依赖于前一个控件的基本位置,是布局最常用,也是最灵活的一种布局
相对布局里面的子元素不像线性布局里面的元素,不设置相关属性他们就会以父元素的左上角为顶点叠加显示

子元素居中 layout_centerInParent

<?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:background="@android:color/white">
  <TextView
      android:id="@+id/middle"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true"
      android:background="@android:color/holo_blue_bright"
      android:text="我在最中间"/>
</RelativeLayout>

《相对布局(RelativeLayout)》 效果.png

相对布局位置相对属性都有一组,像layout_centerInParent这样的性对于父元素的位置的属性还有
android:layout_centerHorizontal="true"  相对于父元素在水平方向居中
android:layout_centerVertical="true"    相对于父元素垂直方向居中
android:layout_alignParentBottom="true" 相对于父元素居底部
android:layout_alignParentTop="true"    相对于父元素居顶部
android:layout_alignParentLeft="true"   相对于父元素居左部
android:layout_alignParentRight="true"  相对于父元素居右部
android:layout_alignParentEnd="true"    相对于父元素居右部 和 layout_alignParentRight 效果相同 api>=17
android:layout_alignParentStart="true"  相对于父元素居左部 和 layout_alignParentLeft  效果相同 api>=17

上面这些总结的属性都是相对于父元素的位置

相对于元素外部四周

<?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:background="@android:color/white">
  <TextView
      android:id="@+id/middle"
      android:layout_width="200dp"
      android:layout_height="200dp"
      android:gravity="center"
      android:layout_centerInParent="true"
      android:background="@android:color/holo_blue_bright"
      android:text="我在最中间"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/middle"
        android:text="layout_below"
        android:layout_centerHorizontal="true"
        android:background="@android:color/holo_green_light"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/middle"
        android:text="layout_above"
        android:layout_centerHorizontal="true"
        android:background="@android:color/holo_green_light"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/middle"
        android:text="layout_toLeftOf"
        android:layout_centerVertical="true"
        android:background="@android:color/holo_green_light"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/middle"
        android:text="layout_toRightOf"
        android:layout_centerVertical="true"
        android:background="@android:color/holo_green_light"/>
</RelativeLayout>

《相对布局(RelativeLayout)》 效果.png

 android:layout_below="@id/middle"       相对位置居元素外部的上方
 android:layout_above="@id/middle"       相对位置居元素外部的下方
 android:layout_toLeftOf="@id/middle"    相对位置居元素外部的左方
 android:layout_toRightOf="@id/middle"   相对位置居元素外部的右方

上面这些总结的属性都是相对于子元素外部的位置

相对于元素内部四周

<?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:background="@android:color/white">
  <TextView
      android:id="@+id/middle"
      android:layout_width="200dp"
      android:layout_height="200dp"
      android:gravity="center"
      android:layout_centerInParent="true"
      android:background="@android:color/holo_blue_bright"
      android:text="我在最中间"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/middle"
        android:text="layout_alignBottom"
        android:layout_centerHorizontal="true"
        android:background="@android:color/holo_orange_light"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/middle"
        android:text="layout_alignTop"
        android:layout_centerHorizontal="true"
        android:background="@android:color/holo_orange_light"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/middle"
        android:text="alignLeft"
        android:layout_centerVertical="true"
        android:background="@android:color/holo_orange_light"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@id/middle"
        android:text="alignRight"
        android:layout_centerVertical="true"
        android:background="@android:color/holo_orange_light"/>
</RelativeLayout>

《相对布局(RelativeLayout)》 效果.png

android:layout_alignBottom="@id/middle"  相对位置居元素内部的下方
android:layout_alignTop="@id/middle"     相对位置居元素内部的上方
android:layout_alignLeft="@id/middle"    相对位置居元素内部的左边
android:layout_alignRight="@id/middle"   相对位置居元素内部的右边

上面这些总结的属性都是相对于子元素内部的位置

代码只会按照你所写的方式运行,不会按照你想的方式运行

    原文作者:a紫星
    原文地址: https://www.jianshu.com/p/13dc7ad66eb1
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞