<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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="com.yunlibeauty.test.RelativeLayoutDemo"> <View android:id="@+id/center" android:layout_width="50dp" android:layout_height="50dp" android:layout_centerInParent="true" android:background="@color/colorPrimary" /> <View android:id="@+id/above" android:layout_width="50dp" android:layout_height="50dp" android:layout_above="@id/center" android:layout_centerHorizontal="true" android:background="@color/colorC" /> <View android:id="@+id/left" android:layout_width="50dp" android:layout_height="50dp" android:layout_toLeftOf="@id/center" android:layout_centerVertical="true" android:background="@color/colorB"/> <View android:id="@+id/right" android:layout_width="50dp" android:layout_height="50dp" android:layout_toRightOf="@id/center" android:layout_centerVertical="true" android:background="@color/colorA"/> <View android:id="@+id/below" android:layout_width="50dp" android:layout_height="50dp" android:layout_below="@id/center" android:layout_centerHorizontal="true" android:background="@color/colorD"/> </RelativeLayout>这是一个梅花图案
首先我定义了一个id为center的View,设置了layout_centerInParent为true,顾名思义这个属性是相对于父view放置到中间然后其他left、right、above、below都是相对于center View来放置的
那好,我详细介绍下这个RelativeLayout里的属性值,
1:基本属性:与LinearLayout一样,也有gravity与ignoreGravity属性,gravity属性设置了容器内属性的对齐方式,ignoreGravity改组件不受gravity属性的控制
2:根据父容器的定位:android:layout_alignParentLeft="true" --------与父容器左对齐 android:layout_alignParentRight="true"--------与父容器右对齐 android:layout_alignParentTop="true"----------与父容器顶部对齐 android:layout_alignParentBottom="true"-------与父容器底部对齐 android:layout_alignParentStart="true"--------与父容器开始对齐\与父容器左对齐,与layout_alignParentLeft一样,api17以后新出的 android:layout_centerHorizontal="true"--------与父容器水平居中 android:layout_centerVertical="true"----------与父容器竖直居中 android:layout_centerInParent="true"----------父容器中心
3:根据兄弟容器定位:android:layout_toLeftOf="@id/center"----------置于center左边android:layout_toRightOf="@id/center"---------置于center右边 android:layout_toStartOf="@+id/center"--------同layout_toLeftOf右边API 17后可以用 android:layout_toEndOf="@id/center"-----------同layout_toRightOf右边API 17后可以用 android:layout_above="@id/center"-------------置于center上边 android:layout_below="@id/center"-------------置于center下边 android:layout_alignLeft="@id/center"---------参考center左边距,即与center左对齐 android:layout_alignRight="@id/center"--------参考center右边距,即与center右对齐 android:layout_alignTop="@id/center"----------参考center上边距,即与center顶部对齐 android:layout_alignBottom="@id/center"-------参考center下边距,即与center底部对齐
3:Margin组件整体偏移量:android:layout_marginLeft="10dp" -----------向左偏移 android:layout_marginRight="10dp"-----------向右偏移 android:layout_marginTop="10dp"-------------离上边组件的偏移 android:layout_marginBottom="10dp"----------离下边组件的偏移 android:layout_marginStart="10dp"-----------同layout_marginLeft android:layout_marginEnd="10dp"-------------同layout_marginRight android:layout_marginHorizontal="10dp"------水平向右偏移 android:layout_marginVertical="10dp"--------竖直向下偏移4:Padding组件内部元素偏移量android:paddingLeft="10dp" android:paddingRight="10dp" android:paddingTop="10dp" android:paddingBottom="10dp" android:paddingStart="10dp" android:paddingEnd="10dp" android:paddingHorizontal="10dp" android:paddingVertical="10dp"这个就不解释了,基本都一样Margin与Padding的区别就在于,Margin是相对于相邻的组件的便宜量,而Padding是该组件内部的偏移量,如果还是不清楚就写个demo试下哦
2:RelativeLayout
最新推荐文章于 2022-10-20 09:53:32 发布