目录
布局管理器
Android提供五种布局管理器:
1.相对布局管理器(RelativeLayout):通过相对定位的方式来控制组件的摆放位置。
2.线性布局管理器(LinearLayout):是指在水平或者垂直方向上依次摆放组件。
3.帧布局管理器(FrameLayout):没有任何定位方式,默认情况下,所有的组件都会摆放在容器的左上角,逐个覆盖。
4.表格布局管理器(TableLayout):使用表格的方式按行、列来摆放组件。
5.绝对布局管理器(AbsoluteLayout):通过绝对定位(x,y坐标)的方式来控制组件的摆放位置。(过期)
6.网格布局管理器(GridLayout):通过它可以实现跨行和跨列摆放组件。
Android提供的布局管理器均直接或间接地继承ViewGroup类。
说明:
在Android中,无论是创建哪一种布局都有两种方法,一种是在XML布局文件当中定义,另一种是使用Java代码来创建。推荐使用XML布局文件中定义。
RelativeLayout
常见属性
android:layout_above="@id/xxx" --将控件置于给定ID控件之上
android:layout_below="@id/xxx" --将控件置于给定ID控件之下
android:layout_toLeftOf="@id/xxx" --将控件的右边缘和给定ID控件的左边缘对齐
android:layout_toRightOf="@id/xxx" --将控件的左边缘和给定ID控件的右边缘对齐
android:layout_alignLeft="@id/xxx" --将控件的左边缘和给定ID控件的左边缘对齐
android:layout_alignTop="@id/xxx" --将控件的上边缘和给定ID控件的上边缘对齐
android:layout_alignRight="@id/xxx" --将控件的右边缘和给定ID控件的右边缘对齐
android:layout_alignBottom="@id/xxx" --将控件的底边缘和给定ID控件的底边缘对齐
android:layout_alignParentLeft="true" --将控件的左边缘和父控件的左边缘对齐
android:layout_alignParentTop="true" --将控件的上边缘和父控件的上边缘对齐
android:layout_alignParentRight="true" --将控件的右边缘和父控件的右边缘对齐
android:layout_alignParentBottom="true" --将控件的底边缘和父控件的底边缘对齐
android:layout_centerInParent="true" --将控件置于父控件的中心位置
android:layout_centerHorizontal="true" --将控件置于水平方向的中心位置
android:layout_centerVertical="true" --将控件置于垂直方向的中心位置
Relative的实践操作(实现软件更新界面)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="@color/design_default_color_secondary_variant"
tools:context=".MainActivity"
android:padding="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发现有Widget的新版本,您现在就安装吗?"
android:id="@+id/text1"
android:layout_centerInParent="true">
</TextView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:text="以后再说"
android:layout_alignRight="@+id/text1"
android:layout_below="@+id/text1">
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="现在更新"
android:layout_toLeftOf="@+id/button1"
android:layout_below="@+id/text1">
</Button>
</RelativeLayout>
LinearLayout
常见属性
1.layout_gravity 设置布局中控件的位置 top 上 bottom 下 left 左 right 右 center_vertical 垂直居中 center_horizontal 水平居中 center 居中
2.weight(权重)属性
用来设置占布局所占布局得的比重 (1)layout_width都为0时 按照所设置的比重来分配权重 比如:三个控件 1 2 3 weight分比为1、2、3 layout_width = "0dp" 则会将屏幕的大小分为1+2+3=6份 三个控件分别占布局的1/6 2/6 3/6
(2)layout_width都为warp_content时