六大布局:线性布局,相对布局,绝对布局,表格布局,帧布局,网格布局。
线性布局(LinearLayout):
<LinerLayout>
作为根标签。线性布局内控件垂直或者水平排列。通过orientation
属性来指定。取值:horizontal(水平)默认值,vertical(垂直)。
栅格化排列:通过layout_height
和layout_width
或layout_height
属性联合控制。
示例:水平方向3个button平分宽度。指定layout_height
都相同,并且layout_width
要为0dp。
效果图:
<?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"
tools:context=".activity.Main4Activity">
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:text="我占1格"
android:textSize="15sp"/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:text="我占1格"
android:textSize="15sp"/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="50dp"
android:text="我占1格"
android:textSize="15sp"/>
</LinearLayout>
解释:
上面示例中我们可以看到一行中3个button的weight加在一起等于3,就说明把宽度平均分成3份,每个button的weight等于1,1/3所以每个button各占一份。当然我们也可以指定让第一个按钮占1格,第二个按钮占2格,第三个按钮占3格。
效果图:
<?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"
tools:context=".activity.Main4Activity">
<Button
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="占1格"
android:textSize="15sp"/>
<Button
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="2"
android:text="占2格"
android:textSize="15sp"/>
<Button
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="3"
android:text="占3格"
android:textSize="15sp"/>
</LinearLayout>
相对布局(RelativeLayout):
<RelativeLayout>
作为根标签。相对于 父控件 或者 指定 id 控件 布局。
布局内控件的常用属性:
居中:
layout_centerVertical
:指定为true时垂直居中。
layout_centerHorizontal
:指定为true时水平居中。
相对于 父控件:
layout_alignParentTop
:和父控件顶部对齐。
layout_alignParentRight
:和父控件右侧对齐。
layout_alignParentBottom
:和父控件底部对齐。
layout_alignParentLeft
:和父控件左侧对齐。
相对于 指定 id 控件:
layout_above
:跟随到指定控件上方。
layout_below
:跟随到指定控件下方。
layout_toRightOf
:跟随到指定控件右侧。
layout_toLeftOf
:跟随到指定控件左侧。
示例:
效果图:
<?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=".activity.Main2Activity">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="按钮1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/btn1"
android:text="按钮2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_toRightOf="@+id/btn1"
android:text="按钮3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="@+id/btn1"
android:text="按钮4"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/btn1"
layout
android:text="按钮5"/>
</RelativeLayout>
效果图:
<?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=".activity.Main2Activity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="按钮1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="按钮2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:text="按钮3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="按钮3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:text="按钮3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="按钮3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮3"/>
</RelativeLayout>
绝对布局(AbsoluteLayout):
<AbsoluteLayout>
作为根标签。控件通过 layout_x
和layout_y
来指定其位置。
注:AbsoluteLayout is deprecated.
表格布局(TableLayout):
<TableLayout>
作为根标签。在<TableLayout>
标签内使用<TableRow>
标签来组织控件。控件对lay_weight
属性生效。
效果图:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
tools:cotext=".MainActivity">
<TableRow>
<Button android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent"/>
<Button android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent"/>
<Button android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent"/>
<Button android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent"/>
<Button android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent"/>
</TableRow>
</TableLayout>