Android中常用的5大布局方式有以下几种:
线性布局(LinearLayout):按照垂直或者水平方向布局的组件。 帧布局(FrameLayout):组件从屏幕左上方布局组件。 表格布局(TableLayout):按照行列方式布局组件。 相对布局(RelativeLayout):相对其它组件的布局方式。 绝对布局(AbsoluteLayout):按照绝对坐标来布局组件。1. 线性布局
线性布局是Android开发中最常见的一种布局方式,它是按照垂直或者水平方向来布局,通过“android:orientation”属性可以设置线性布局的方向。属性值有垂直(vertical)和水平(horizontal)两种。
常用的属性:
android:orientation:可以设置布局的方向 android:gravity:用来控制组件的对齐方式 layout_weight:控制各个组件在布局中的相对大小
线性布局,两种排法:
从左到右 android:orientation=”horizontal” 从上到下 android:orientation=”vertical”第一个实例
①效果图:
②核心代码如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="250dp" android:layout_height="250dp"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮3" android:layout_alignParentRight="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮2" android:layout_centerInParent="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮4" android:layout_alignParentBottom="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮5" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" /> </RelativeLayout> </RelativeLayout>帧布局 帧布局是从屏幕的左上角(0,0)坐标开始布局,多个组件层叠排列,第一个添加的组件放到最底层,最后添加到框架中的视图显示在最上面。上一层的会覆盖下一层的控件。
简单的例子 ①效果图:
② 核心代码:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="" android:background="#ff0000" /> <TextView android:layout_width="510dp" android:layout_height="290dp" android:layout_gravity="center" android:text="" android:background="#FFA500" /> <TextView android:layout_width="450dp" android:layout_height="260dp" android:layout_gravity="center" android:text="" android:background="#FFFF00" /> <TextView android:layout_width="380dp" android:layout_height="220dp" android:layout_gravity="center" android:background="#008000" /> <TextView android:layout_width="310dp" android:layout_height="190dp" android:layout_gravity="center" android:background="#E0FFFF" /> <TextView android:layout_width="260dp" android:layout_height="150dp" android:layout_gravity="center" android:background="@drawable/a1" /> </FrameLayout>3.表格布局 表格布局是一个ViewGroup以表格显示它的子视图(view)元素,即行和列标识一个视图的位置。 表格布局常用的属性如下: android:collapseColumns:隐藏指定的列 android:shrinkColumns:收缩指定的列以适合屏幕,不会挤出屏幕 android:stretchColumns:尽量把指定的列填充空白部分 android:layout_column:控件放在指定的列 android:layout_span:该控件所跨越的列数
简单的列子: ①效果图:
② 核心代码:
<?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" android:background="#000000"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是第一个lay的第一行" android:background="#AF2220" /> <TableRow> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="2行1列" android:background="#AD0000" android:layout_weight="1" /> <TextView android:layout_width="180dp" android:layout_height="wrap_content" android:background="#FFFFFF" android:text="2行2列" /> <TextView android:layout_width="150dp" android:layout_height="wrap_content" android:text="2行3列" android:background="#AD00AD" /> </TableRow> <TableLayout android:collapseColumns="1" > <TableRow> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第二个layout的第1行第1列" android:background="#45ED2E" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第二个layout的第1行第2列" android:background="#9C00FF" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第二个layout的第1行第3列" android:background="#9C00FF" /> </TableRow> <TableRow> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="1行第1列" android:background="#20FF00" /><TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="1行第2列" android:background="#aa00ff" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="1行第3列" android:background="#A02EEC" /> </TableRow> </TableLayout> </TableLayout>4.相对布局 相对布局是按照组件之间的相对位置来布局,比如在某个组件的左边,右边,上面和下面等。 参考其他控件进行布局,默认为父控件。 有三种类型的属性:
属性值是true或false android:layout_centerHrizontal 水平居中 android:layout_centerVertical 垂直居中 android:layout_centerInparent 相对于父元素完全居中。 android:layout_alignParentBottom 位于父元素的下边缘 android:layout_alignParentTop 位于父元素的上边缘 android:layout_alignParentLeft 位于父元素的左边缘 android:layout_alignParentRight 位于父元素的右边缘 属性值是”@id/*“ android:layout_below 在某元素的下方 android:layout_above 在某元素的上方 andorid:layout_toRightOf 在某元素的右方 android:layout_toLeftOf 在某元素的左方 android:layout_alignBottom 和某元素下方对齐 android:layout_alignTop 和某元素上方对齐 android:layout_alignRight 和某元素右方对齐 android:layout_alignLeft 和某元素左方对齐 属性值是数值 android:layout_marginLeft 离某元素左边缘的距离 android:layout_marginRight 离某元素右边缘的距离 android:layout_marginTop 离某元素上边缘的距离 android:layout_marginBottom 离某元素下边缘的距离简单的例子 ①效果图:
② 核心代码:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="250dp" android:layout_height="250dp"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮3" android:layout_alignParentRight="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮2" android:layout_centerInParent="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮4" android:layout_alignParentBottom="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮5" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" /> </RelativeLayout> </RelativeLayout>5.网格布局
<?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:rowCount="2" android:columnCount="4" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2" /> <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:text="/" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="4" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="5" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="6" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="x" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="7" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="8" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="9" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" android:layout_columnSpan="2" android:layout_gravity="fill_horizontal" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="." /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+" android:layout_rowSpan="2" android:layout_gravity="fill_vertical" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="=" android:layout_columnSpan="3" android:layout_gravity="fill_horizontal" /> <Space /> </GridLayout>6. 绝对布局 绝对布局通过指定子组件的确切X,Y坐标来确定组件的位置,在Android2.0 API文档中标明该类已经过期,可以使用FrameLayout或者RelativeLayout来代替。所以这里不再详细介绍。