Android提供了5种类型的布局类型:
View Code
View Code
View Code
View Code
View Code
1:LinearLayout(线性布局)流式布局
Tip:下一个控件的坐标原点由上一个控件来决定,你可以沿水平方向或者垂直方向上来排列你的控件。
<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" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="com.example.lizelu.userinterfacedemo.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"> <!--垂直线性布局方式--> <LinearLayout android:layout_width="pt" android:layout_height="match_parent" android:background="#ff" android:orientation="vertical"> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="pt" android:background="#ff" android:orientation="horizontal"> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff" android:orientation="horizontal"> </LinearLayout> </LinearLayout> </LinearLayout> </RelativeLayout>
2:RelativeLayout(相对布局)Activity默认布局方式
Tip:相对布局可以根据已经固定的控件来确定其他新加控件的位置。
<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="com.example.lizelu.userinterfacedemo.MainActivity"> <Button android:id="@+id/button_center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="center"/> <Button android:id="@+id/button_above" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/button_center" android:layout_centerInParent="true" android:text="above"/> <Button android:id="@+id/button_below" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button_center" android:layout_centerInParent="true" android:text="below"/> <Button android:id="@+id/button_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@+id/button_center" android:layout_centerVertical="true" android:text="left"/> <Button android:id="@+id/button_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/button_center" android:layout_centerVertical="true" android:text="right"/> </RelativeLayout>
3:TableLayout(表格布局)
Tip:表格布局中,整个页面就相当于一张大的表格,控件就放在每个Cell中
<TableLayout android:layout_width="match_parent" android:layout_height="match_parent" android:stretchColumns=""> <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名:"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入用户名"/> </TableRow> <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密 码:"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码" android:inputType="textPassword"/> </TableRow> <TableRow> <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="登录" android:layout_span=""/> </TableRow> </TableLayout>
4:AbsoluteLayout(绝对布局)
Tip:指定组件的左上角绝对坐标来指定组件的布局
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮1" android:layout_x="0px" android:layout_y="0px" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮2" android:layout_x="20px" android:layout_y="20px" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮3" android:layout_x="40px" android:layout_y="40px" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮4" android:layout_x="60px" android:layout_y="60px" /> </AbsoluteLayout>
5:FrameLayout(单帧布局)
Tip:FrameLayout中的Frame和iOS中的Frame不是一个概念,在iOS中的Frame你可以指定任意的坐标,而这个坐标点时相对于父视图的。FrameLayout中的Frame的坐标原点是屏幕的左上角,位置固定,你只需为控件指定大小即可
<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" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="com.example.lizelu.userinterfacedemo.MainActivity"> <FrameLayout android:layout_width="pt" android:layout_height="pt" android:background="#ff"> <FrameLayout android:layout_width="pt" android:layout_height="pt" android:background="#ff"> </FrameLayout> <FrameLayout android:layout_width="pt" android:layout_height="pt" android:background="#ff"> </FrameLayout> <FrameLayout android:layout_width="pt" android:layout_height="pt" android:background="#ffff"> </FrameLayout> <FrameLayout android:layout_width="pt" android:layout_height="pt" android:background="#"> </FrameLayout> </FrameLayout> </RelativeLayout>