布局也属于一种"控件",所有控件的父类就是View控件
1 线性布局
各个子组件之间呈线性(垂直/水平)排列
1.1 常用属性
android:id | 布局id | |
android:layout_width | 宽度 | wrap_content匹配内容 match_parent匹配父控件 |
android:layout_height | 高度 | |
android:background | 背景 | |
android:layout_margin | 外边距 | |
android:padding | 内边距 | 也可以单独设置某一方向 paddingLeft paddingRight..... |
android:gravity | 内部子元素排布方式 | top上 bottom下 left左 right右 center居中 center_vertical垂直居中 |
android:weight | 权重 | 2个子控件各占1就表示平分父控件剩余位置(通常将宽度/高度设置为0),一个占1一个占2就表示前者1/3后者2/3 |
android:orientation | 线性方向(线性布局特有) | vertical(默认) horizon |
1.2 代码示例
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="xyz.aboluo.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:paddingTop="10dp"
android:paddingBottom="20dp"
android:paddingLeft="40dp"
android:paddingRight="90dp"
android:background="#00FF00">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:background="#00FFFF">
<LinearLayout
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#000000"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:background="#4098d6">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ff9900"
tools:ignore="Suspicious0dp" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="#956fe7"
tools:ignore="Suspicious0dp" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ed7976"
tools:ignore="Suspicious0dp" />
</LinearLayout>
</LinearLayout>
2 相对布局
各个子组件中,一个组件相对于另一个组件的位置
2.1 常用属性
android:layout_toLeftOf | 在who左边 |
android:layout_toRightOf | 在who右边 |
android:layout_below | 在who下面 |
android:layout_alignBottom | 与who底部对齐 |
android:layout_alignParentBottom | 与父控件底部对齐 (还有alignParentRight等等.....) |
2.2 代码示例
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="xyz.aboluo.MainActivity">
<View
android:id="@+id/view_1"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#000000"
android:layout_alignParentRight="true"/>
<View
android:id="@+id/view_2"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#FF0000"
android:layout_below="@id/view_1"/>
</RelativeLayout>