相对布局RelativeLayout
#常用属性如下:
分别是在左边、在右边、底部对齐,和父控件底部对齐、在某个控件的下面
这些都是相对布局特有的属性。
#关于alignParentBottom:
该属性顾名思义就是,对齐与父元素的底部,
定义一个小黑框,并让他alignParentBottom的value为true,
那么效果就是
这样,小黑块默认出现在底部的左边,若想出现在右边,我们可以利用下面这个属性?
#关于对齐父元素右边——layout_alignParentRight
还是使用上面的小黑块,我们添加layout_alignParentRight属性为ture,
如下,这个小黑块就到了父元素的底部的右边,当然如果取出上面那句对齐
父元素底部的属性的话,那就是在父元素上面的右边了。
#关于:放置元素于XX元素上下左右的属性——
layout_toRightOf\layout_below
这个属性等号后面是空间的id,属性的意思是,把当前控件放置在XX(后面跟随的id的控件)的
控件的右边。比如这样。
我想把id为View_2放在View_1的右边。那么就照着上面这么一通操作(逃
于是。小蓝块就到了小黑块的右边。如下,一家人被安排的整整齐齐。
那么,我想放在小黑的下面也应该有对应的方法吧?那就是 layout_below。
使用方法也是一样,在当前控件下写这个属性,后面跟着相对应的元素的id:XX,那么当前控件就放在了XX的下面
#练习代码:
<?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">
<!--默认方向垂直-->
<View android:id="@+id/view_1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#000000"
>
</View>
<!---->
<View android:id="@+id/view_2"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#0000ff"
android:layout_toRightOf="@id/view_1"
android:layout_below="@id/view_1"
>
</View>
<!---->
<LinearLayout
android:id="@+id/ll_1"
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="horizontal"
android:background="#0066ff"
android:padding="15dp"
>
<View android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#ffffff"
>
</View>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:padding="15dp"
>
<View
android:id="@+id/view_3"
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#ff9900">
</View>
<View
android:id="@+id/view_4"
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#ff0055"
android:layout_toRightOf="@id/view_3"
android:layout_marginLeft="10dp">
</View>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
#实现目标效果:
总结:
相对布局通过相对定位的方式,灵活的让控件出现在布局的任何位置,通过 layout_toRightOf、
layout_below、toLeftof......属性,配合padding和margin,我们 可以摆出想要的布局。是一个十分
常用的布局方式。?