RelativeLayout布局基础知识详解
每个属性只举1-2个例子哦,操作都是差不多的,如果还有问题的话,欢迎留言😄
1.基本属性
①gravity用于设置容器内组件的对齐方式
- center中间
- left左边
- right右边
- top上边
- bottom下边
话不多说,上图,上代码!!!
<?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="#BBFFFF"
android:gravity="center">
<View
android:id="@+id/view_1"
android:layout_height="100dp"
android:layout_width="100dp"
android:background="#FFF5EE"
>
</View>
</RelativeLayout>
②ignoreGravity若组件这一属性设置为true,那么这一组件将不会受到gravity属性的影响
2.根据父容器定位
①layout_alignParentLeft和父容器左对齐
话不多说,上图,上代码!!!
<?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="#BBFFFF">
<View
android:id="@+id/view_1"
android:layout_height="100dp"
android:layout_width="100dp"
android:background="#000000"
android:layout_alignParentLeft="true"
>
</View>
</RelativeLayout>
②layout_alignParentRight和父容器右对齐
③layout_alignParentTop和父容器顶部对齐
④layout_alignParentBottom和父容器底部对齐
⑤layout_centerHorizontal水平居中
⑥layout_centerVertical垂直居中
⑦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="#BBFFFF">
<View
android:id="@+id/view_1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:background="#FFF5EE">
</View>
</RelativeLayout>
3.根据兄弟组件定位
①layout_toLeftOf兄弟组件的左边
话不多说,上图,上代码!!!
<?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="#BBFFFF">
<View
android:id="@+id/view_1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#FFF5EE">
</View>
<View
android:id="@+id/view_2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_toRightOf="@+id/view_1"
android:background="#7FFFD4">
</View>
</RelativeLayout>
②layout_toRightOf兄弟组件的右边
③layout_above兄弟组件的上面
④layout_below兄弟组件的下面
⑤layout_alignTop对齐兄弟组件的上边界
⑥layout_alignBottom对齐兄弟组件的下边界
⑦layout_alignRight对齐兄弟组件的右边界
⑧layout_alignLeft对齐兄弟组件的左边界
4.外间距(margin)
①layout_margin设置组件上下左右的间距
话不多说,上图,上代码!!!
<?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:layout_margin="30dp"
android:background="#BBFFFF">
<View
android:id="@+id/view_1"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
android:background="#000000">
</View>
<View
android:id="@+id/view_2"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#7FFFD4">
</View>
</RelativeLayout>
②layout_marginLeft设置组件离左边的间距
③layout_marginRight设置组件离右边的间距
④layout_marginTop设置组件离上方的间距
⑤layout_marginBottom设置组件离下方的间距
5.内边距(padding)
①padding内部组件的上下左右填充一定边距
话不多说,上图,上代码!!!
<?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:layout_margin="30dp"
android:background="#BBFFFF"
android:paddingTop="30dp">
<View
android:id="@+id/view_1"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#000000">
</View>
<View
android:id="@+id/view_2"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_below="@+id/view_1"
android:background="#7FFFD4">
</View>
</RelativeLayout>
②paddingLeft内部组件的左边填充一定的边距
③paddingRight内部组件右边填充一定的边距
④paddingTop内部组件上方填充一定的边距
⑤paddingBottom内部组件下方填充一定的边距
最后的最后再来个嵌套的供大家加深理解!!!
<?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:layout_margin="30dp"
android:background="#BBFFFF">
<View
android:id="@+id/view_1"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#000000">
</View>
<View
android:id="@+id/view_2"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="@+id/view_1"
android:background="#7FFFD4" />
<RelativeLayout
android:id="@+id/view_3"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="@+id/view_2"
android:background="#9370DB"
android:padding="15dp">
<View
android:id="@+id/view_4"
android:layout_width="150dp"
android:layout_height="match_parent"
android:background="#CD7054">
</View>
<View
android:layout_width="150dp"
android:layout_height="match_parent"
android:background="#CD7054"
android:layout_toRightOf="@+id/view_4"
android:layout_marginLeft="20dp">
</View>
</RelativeLayout>
</RelativeLayout>