600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > Android中常见五种布局管理器——RelativeLayout LinearLayout FrameLayout TableLayout GridLayout

Android中常见五种布局管理器——RelativeLayout LinearLayout FrameLayout TableLayout GridLayout

时间:2019-05-20 12:02:47

相关推荐

Android中常见五种布局管理器——RelativeLayout LinearLayout FrameLayout TableLayout GridLayout

目录

布局管理器

RelativeLayout

常见属性

Relative的实践操作(实现软件更新界面)

LinearLayout

常见属性

LinearLayout的实践操作(模范登录以及微信底部)

FrameLayout

常用属性

FrameLayout实例

TableLayout

常用属性(继承LinearLayout)

TableLayout实例(计算器·的实例)

GridLayout

常用属性

GridLayout实例

网格布局管理器和表格布局管理器的区别:

布局管理器

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="/apk/res/android"xmlns:app="/apk/res-auto"xmlns:tools="/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">​​<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="发现有Widget的新版本,您现在就安装吗?"android:id="@+id/text1"android:layout_centerInParent="true">​</TextView><Buttonandroid: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><Buttonandroid: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

LinearLayout的实践操作(模范登录以及微信底部)

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"xmlns:app="/apk/res-auto"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:id="@+id/line"​tools:context=".LinearLayout">​<EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:paddingBottom="20dp"android:hint="QQ号/微信号/Email"></EditText>​<EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:paddingBottom="20dp"android:hint="密码"></EditText>​<Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="登录"></Button>​<TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="登录遇到问题?"android:gravity="center"></TextView>​<LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"></LinearLayout>​<LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="bottom"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="首页"></Button><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="联系人"></Button><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="朋友圈"></Button><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="我"></Button></LinearLayout></LinearLayout>

FrameLayout

常用属性

1.android:foreground :设置该帧布局管理器的前景图像。

2.android:foregroundGravity:定义绘制前景图像的gravity属性,即前景图像显示的位置。

FrameLayout实例

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="/apk/res/android"xmlns:app="/apk/res-auto"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".FrameLayout">​<TextViewandroid:layout_width="280dp"android:layout_height="280dp"android:background="#ff0000ff"android:layout_gravity="center"android:textColor="#ffffff"android:text="蓝色背景板的TextView"></TextView>​<TextViewandroid:layout_width="230dp"android:layout_height="230dp"android:background="#ff0077ff"android:layout_gravity="center"android:textColor="#ffffff"android:text="天蓝色背景板的TextView"></TextView>​<TextViewandroid:layout_width="180dp"android:layout_height="180dp"android:background="#ff00b4ff"android:layout_gravity="center"android:text="水蓝色背景板的TextView"android:textColor="#ffffff"></TextView></FrameLayout>

TableLayout

常用属性(继承LinearLayout)

1.android:collapseColumns:将TableLayout里面指定的列隐藏,若有多列需要隐藏,请用逗号将需要隐藏的列序号隔开。

2.android:stretchColumns:设置指定的列为可伸展的列,以填满剩下的多余空白空间,若有多列需要设置为可伸展,请用逗号将需要伸展的列序号隔开。

3.android:shrinkColumns:设置指定的列为可收缩的列。当可收缩的列太宽(内容过多)不会被挤出屏幕。当需要设置多列为可收缩时,将列序号用逗号隔开。

TableLayout实例(计算器·的实例)

<?xml version="1.0" encoding="utf-8"?><TableLayout xmlns:android="/apk/res/android"xmlns:app="/apk/res-auto"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".TableLayout"><EditTextandroid:layout_height="80dp"android:hint="0"android:layout_width="match_parent"></EditText><TableRowandroid:layout_width="wrap_content"android:layout_height="60dp"android:padding="10dp"><Buttonandroid:layout_height="match_parent"android:layout_width="wrap_content"android:layout_weight="1"android:text="AC"></Button>​<Buttonandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="DEL"></Button><Buttonandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="%"></Button><Buttonandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="*"></Button></TableRow><TableRowandroid:layout_width="wrap_content"android:layout_height="60dp"android:padding="10dp"><Buttonandroid:layout_height="match_parent"android:layout_width="wrap_content"android:layout_weight="1"android:text="7"></Button>​<Buttonandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="8"></Button><Buttonandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="9"></Button><Buttonandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="/"></Button></TableRow><TableRowandroid:layout_width="wrap_content"android:layout_height="60dp"android:padding="10dp"><Buttonandroid:layout_height="match_parent"android:layout_width="wrap_content"android:layout_weight="1"android:text="4"></Button>​<Buttonandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="5"></Button><Buttonandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="6"></Button><Buttonandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="+"></Button></TableRow><TableRowandroid:layout_width="wrap_content"android:layout_height="60dp"android:padding="10dp"><Buttonandroid:layout_height="match_parent"android:layout_width="wrap_content"android:layout_weight="1"android:text="1"></Button>​<Buttonandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="2"></Button><Buttonandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="3"></Button><Buttonandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="-"></Button></TableRow><TableRowandroid:layout_width="wrap_content"android:layout_height="60dp"android:padding="10dp"><Buttonandroid:layout_height="match_parent"android:layout_width="wrap_content"android:layout_weight="1"android:text="0"></Button>​<Buttonandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:text="."></Button><Buttonandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_span="2"android:layout_weight="1"android:text="="></Button></TableRow></TableLayout>

GridLayout

常用属性

1.android:columnCount: 最大列数

2.android:rowCount:最大行数

3.android:orientation: GridLayout中子元素的布局方向

4.android:alignmentMode alignBounds:对齐子视图边界 alignMargins :对齐子视距内容,默认值

5.android:columnOrderPreserved: 使列边界显示的顺序和列索引的顺序相同,默认是true

6.android:rowOrderPreserved: 使行边界显示的顺序和行索引的顺序相同,默认是true

7.android:useDefaultMargins: 没有指定视图的布局参数时使用默认的边距,默认值是false

GridLayout实例

<?xml version="1.0" encoding="utf-8"?><GridLayout xmlns:android="/apk/res/android"xmlns:app="/apk/res-auto"xmlns:tools="/tools"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"android:columnCount="4"android:rowCount="5"tools:context=".GridLayout">​<Buttonandroid:text="1"></Button><Buttonandroid:text="2"></Button><Buttonandroid:text="3"></Button><Buttonandroid:text="/"></Button><Buttonandroid:text="4"></Button><Buttonandroid:text="5"></Button><Buttonandroid:text="6"></Button><Buttonandroid:text="*"></Button><Buttonandroid:text="7"></Button><Buttonandroid:text="8"></Button><Buttonandroid:text="9"></Button><Buttonandroid:text="-"></Button><Buttonandroid:layout_columnSpan="2"android:layout_gravity="fill"android:text="0"></Button><Buttonandroid:text="."></Button><Buttonandroid:layout_rowSpan="2"android:layout_gravity="fill_vertical"android:text="+"></Button><Buttonandroid:text="="android:layout_columnSpan="3"android:layout_gravity="fill"></Button></GridLayout>

网格布局管理器和表格布局管理器的区别:

1.网格布局管理器可以跨行或者跨列,但是表格布局管理器只能和跨行。

2.网格布局管理器可以实现一行占满后超出容器的组件将自动换行,而表格布局管理器超出容器的组件将不会被显示。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。