2:RelativeLayout
2017-12-04 17:25
134 查看
<?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=< 4000 /strong>[b]"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_marginLeftandroid:layout_marginEnd="10dp"-------------同layout_marginRightandroid: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试下哦
相关文章推荐
- android中RelativeLayout相关的属性
- RelativeLayout
- relativelayout中图片和文字居中出现错误
- Android控件 - RelativeLayout 简介
- RelativeLayout的各个属性
- RelativeLayout常用属性介绍
- 在代码动态设置RelativeLayout的属性,比如layout_below
- RelativeLayout加上ListView的item点击变色效果
- SlidingMenu_RelativeLayout_CustomViewBehind
- RelativeLayout的基本对齐方式
- Android五大布局-LinearLayout, FrameLayout, AbsoluteLayout, RelativeLayout
- RelativeLayout的几个关键属性
- Android中Glide加载到RelativeLayout背景图方法示例
- RelativeLayout总结
- RelativeLayout布局,以及部分常用属性介绍scaleType/gravity/layout_gravity [zt]
- android LinearLayout和RelativeLayout实现精确布局
- Android Layout 之 RelativeLayout,代码实现相对布局
- RelativeLayout布局属性详解
- Android学习-RelativeLayout相对布局属性方法
- Android Layout 之 RelativeLayout,代码实现相对布局