Android布局之相对布局
1. 相对布局
1.1 相对布局窗口内子组件的位置总是相对兄弟组件、父容器来决定的,因此叫相对布局
1.2 如果A组件位置是由B组件的位置决定的,Android要求先定B组件,再定义A组件
如果A组件位置是由B组件的位置决定的,Android要求先定B组件,再定义A组件
如果A组件位置是由B组件的位置决定的,Android要求先定B组件,再定义A组件
注1:注意XML中组件的顺序,不然会报错
注2:android新版本中组件的定义顺序没有关系
1.3 RelativeLayout支持的二个xml属性
1.3.1 android:gravity :设置该布局容器内各子组件的对齐方式
1.3.2 android:ignoreGravity:设置哪个组件不受gravity属性的影响
1.4 控制子组件布局的内部类RalativeLayout.LayoutParams
此内部类的属性分二类
1.4.1 boolean
相对父元素
alignParent...
center...(只有在父元素中才存在水平或垂直居中等)
1.4.2 id型
@+id和@id的区别
@+id/x1(添加新ID)
@id/x1(引用此ID)
相对于指定元素(根据ID指定)
1.5 layout_toRightOf,layout_toLeftOf(是一种靠拢动作)
相对布局相关代码展示,仅作为参考:
<?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=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_main_1"//定义的id
android:text="按钮1"
/>
<Button
android:id="@+id/btn_main_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/btn_main_1"
android:layout_below="@id/btn_main_1"
android:text="按钮2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/btn_main_2"
android:text="按钮3"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/btn_main_2"
android:layout_below="@id/btn_main_2"
android:text="按钮4"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/btn_main_2"
android:layout_below="@id/btn_main_2"
android:text="按钮5"
/>
</RelativeLayout>
效果图展示: