目录
布局管理器
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时 这种情况下和上边的情况一样
(3)layout_width为match_parent时是最复杂的一种情况 因为此时控件所占的所有宽度大于容器的宽度 此时要计算每个空间所占的比例 计算公式: 额外的空间=手机的宽度(高度)-所有控件的宽度(高度) 控件的宽度(高度)=控件的width(height)值+(该控件的weight值/所有控件的weight的和)×额外的空间
以上边的情况为例子: 设 match_parent为x 额外空间 = x - 3x = -2x 控件1宽度 = x+ 1/6*(-2x) = 4/6x =1/3x 所以控件1占屏幕的1/3
3.添加分割线
(1)通过view添加 <View android:layout_width="match_parent" android:layout_height="1px" android:background="#000000" />
(2)通过背景图片添加 android:divider="@drawable/ktv_line_div" android:showDividers="middle" android:dividerPadding="10dp" 1)android:divider设置作为分割线的图片 2)android:showDividers设置分割线的位置,none(无), begining(开始),end(结束),middle(每两个组件间) 3)dividerPadding设置分割线的Padding

本文详细介绍了Android中的五种布局管理器:RelativeLayout、LinearLayout、FrameLayout、TableLayout和GridLayout,包括它们的常见属性和实践操作,如Relative的软件更新界面实现,LinearLayout的登录和底部栏设计,以及表格布局在计算器实例中的应用。强调了网格布局和表格布局在跨行、跨列及溢出处理上的区别。
最低0.47元/天 解锁文章
6664





