一.线性布局
1.线性布局是Android中较为常用的布局方式,它使用标签表示。
2.线性布局有两种方式指定控件位置,一种是水平方向,一种是竖直方向。
3.水平布局或垂直布局
LinearLayout视图是否按行或列来放置它的子视图由LinearLayout视图的android:orientation属性值来决定。
android:orientation=“vertical” 表示是垂直布局
LinearLayout视图是垂直布局时,子视图按行的顺序从上向下排列(即每行上只有一个子视图),每个子视图根据自己的android: layout_height属性值去占用列中的剩余的空间。
android:orientation=“horizontal” 表示是水平布局
LinearLayout视图是水平布局时,子视图按列的顺序从左向右排列,每个子视图根据自己的android: layout_width属性值去占用行中的剩余的空间。
应用实例
布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_weight="2"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="0dp">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="red"
android:layout_weight="1"
android:background="#FF0000"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="green"
android:layout_weight="1"
android:background="#00FF00"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="blue"
android:layout_weight="1"
android:background="#0000FF" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="yellow"
android:layout_weight="1"
android:background="#FFFF00"/>/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="red"
android:layout_weight="1"
android:background="#FF0000"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="green"
android:layout_weight="1"
android:background="#00FF00"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="blue"
android:layout_weight="1"
android:background="#0000FF"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="yellow"
android:layout_weight="1"
android:background="#FFFF00"/>
</LinearLayout>
</LinearLayout>
二.相对布局
1.相对布局通常有两种形式,一种是相对于容器而言的,一种是相对于控件而言的。
2.相对布局常用属性:
(1)相对父布局的位置:
android:layout_alignParentLeft 是否跟父布局左对齐
android:layout_alignParentTop 是否跟父布局顶部对齐
android:layout_alignParentRight 是否跟父布局右对齐
android:layout_alignParentBottom 是否跟父布局底部对齐
android:layout_centerVertical 在父布局中垂直居中
android:layout_centerHorizontal 在父布局中水平居中
android:layout_centerInParent 在父布局中居中
(2)相对于其他控件的位置:
android:layout_toRightOf 在指定控件的右边
android:layout_toLeftOf 在指定控件的左边
android:layout_above 在指定控件的上边
android:layout_below 在指定控件的下边
android:layout_alignBaseline 跟指定控件水平对齐
android:layout_alignLeft 跟指定控件左对齐
android:layout_alignRight 跟指定控件右对齐
android:layout_alignTop 跟指定控件顶部对齐
android:layout_alignBottom 跟指定控件底部对齐
应用实例
布局代码:
<?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">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2"
android:id="@+id/btn2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button1"
android:layout_toLeftOf="@id/btn2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button3"
android:layout_toRightOf="@id/btn2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="182 27"
android:id="@+id/btn4"
android:layout_below="@id/btn2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Button6"
android:id="@+id/btn6"
android:layout_below="@id/btn4"
android:layout_alignRight="@id/btn2"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button5"
android:layout_below="@id/btn4"
android:layout_toLeftOf="@id/btn6"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button7"
android:layout_below="@id/btn4"
android:layout_toRightOf="@id/btn6"/>
</RelativeLayout>