Android学习|布局—— RelativeLayout悬浮布局
与LinearLayout不同,父级布局包含多个子RelativeLayout布局时,这几个子布局是重叠的,并且最后一个布局位于最上方。类似多个图层的重叠效果。
所以,需要设置相对定位进行位置确定,通过以下属性进行设置。
一、根据父容器定位
- layout_alignParentLeft :左对齐
- layout_alignParentRight :右对齐
- layout_alignParentTop :顶部对齐
- layout_alignParentBottom :底部对齐
- layout_centerHorizontal :水平居中
- layout_centerVertical :垂直居中
- layout_centerlnParent :中间位置根据兄弟组件定位
二、根据兄弟组件定位
此设置需要与兄弟组件的id绑定
- layout_toLeftOf :放置于参考组件的左边
- layout_toRightOf :放置于参考组件的右边
- layout_above :放置于参考组件的上方
- layout_below :放置于参考组件的下方
- layout_alignTop :对齐参考组件的上边界
- layout_alignBottom :对齐参考组件的下边界
- layout_alignLeft :对齐参考组件的左边界
- layout_alignRight :对齐参考组件的右边界
三、通用属性
margin 设置组件与父容器的边距
- layout_margin 上下左右偏移
- layout_marginLeft
- layout_marginRight
- layout_marginTop
- layout_margiBottom
padding设置组件内部元素的边距
四、练习
1、根据父容器定位
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#CCC0E4E3">
<!-- 上方的布局中包含以下三个布局,默认情况下,三个布局是重叠的,并且最后一个布局位于最上方,类似图层的重叠效果-->
<!-- 根据父容器定位 -->
<RelativeLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#45A2D4"
android:layout_alignParentBottom="true"/>
<RelativeLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:background