图文精华

恰卡门户 门户 网络科技 查看内容

android相对布局属性(android相对布局讲解)

2021-10-1 14:08| 发布者: 恰卡优化师| 查看: 43| 评论: 0|原作者: 恰卡技术专员|来自: 互联网

有哪些布局类型?

Android系统中为我们提供的五大布局:LinearLayout(线性布局)、FrameLayout(单帧布局)、AbsoluteLayout(绝对布局)、TablelLayout(表格布局)、RelativeLayout(相对布局)。其中最常用的的是LinearLayout、TablelLayout和RelativeLayout。这些布局都可以嵌套使用。

LinearLayout(线性布局)

线性布局是按照水平或垂直的顺序将子元素(可以是控件或布局)依次按照顺序排列,每一个元素都位于前面一个元素之后。线性布局分为两种:水平方向和垂直方向的布局。分别通过属性android:orientation=”vertical” 和 android:orientation=”horizontal”来设置。

案例代码分析:

<LinearLayout 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:orientation="vertical" android:gravity="center_vertical|center_horizontal" tools:context="zzxb.me.layoutdemo.MainActivity"> <Button android:id="@+id/linearLO" android:text="@string/linear_name" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_weight="4" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/tableLO" android:text="@string/table_name" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_weight="4" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/frameLO" android:text="@string/frame_name" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_weight="4" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/relativeLO" android:text="@string/relative_name" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_weight="4" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/absLO" android:text="@string/abslayout_name" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>

线性布局是用<LinearLayout>标签标示,其中常用的属性:


layout_width/layout_height:设置宽度和高度,其值有:wrap_content(适配内容大小),match_parent(适配父容器大小),此两个属性在各个控件中为通用属性

id:唯一标识该控件值

orientation:设置该布局是水平布局(horizontal)还是纵向布局(vertical)

gravity:设置控件的对齐方式,常用值:center_vertical(纵向居中)|center_horizontal(水平居中)

在<Button>标签中,也同样有id,layout_width以及lay_height属性。同时,还有如下常用属性:

text:设置按钮文字,这里有两种方式,一种是直接硬编码,即直接写内容,例如:

android:text="按钮"

第二种方式是非硬编码方式,是通过配置strings.xml文件来配置,例如:

<resources> <string name="btnText">按钮</string></resources>

然后,通过:

android:text="@string/btnText"

引用。

页面跳转的方式:

Intent intent = new Intent(); intent.setClass(MainActivity.this,LinearActivity.class); startActivity(intent);

TableLayout(表格布局)

表格布局与常见的表格类似,以行、列的形式来管理放入其中的UI组件。表格布局使用<TableLayout>标签定义,在表格布局中,可以添加多个<TableRow>标签占用一行。由于<TableRow>标签也是容器,所以还可以在该标签中添加其他组件,每添加一个组件,表格就会增加一列。在表格布局中,列可以被隐藏,也可以被设置为伸展的,从而填充可利用的屏幕空间,还可以设置为强制收缩,直到表格匹配屏幕大小。

TableLayout跟TableRow 是一组搭配应用的布局,TableLayout置底,TableRow在TableLayout的上方,而Button、TextView等控件就在TableRow之上.TableLayout是一个应用错杂的布局,最简单的用法就仅仅是拖沓控件做出个界面,但实际上,会经常在代码里应用TableLayout,例如做出表格的结果。

重要的几个属性如下:

android:collapseColumns="1,3" 隐藏第二列和第4列的控件 android:stretchColumns="0,2,4" 第一列和三列以及第五列的空白textview被拉伸 android:shrinkColumns="1,3" 第二列和第4列的控件被收缩

案例代码:

<TableLayout android:stretchColumns="0,2,4" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:hint="请输入用户名" android:textSize="15sp" android:layout_margin="6dp" android:background="@drawable/corner_round" android:drawableLeft="@mipmap/account" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:hint="请输入密码" android:layout_margin="6dp" android:textSize="15sp" android:inputType="textPassword" android:background="@drawable/corner_round" android:drawableLeft="@mipmap/passwowrd" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:text="登录" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:text="注册" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" /> </TableRow> </TableLayout>

FrameLayout(帧布局)

帧布局被设计成在一个屏幕区域显示一个单一的项(single item)。通常FrameLayout显示一个单一的子控件,它支持的布局属性不够丰富,一般通过layout_gravity来设置子控件的位置。

FrameLayout的子控件被绘制在一个堆栈中,最近添加进来的子控件在堆栈的顶部。

案例代码:

<FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@mipmap/movie" android:contentDescription="@string/movie_desc" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/button" android:contentDescription="@string/pause_desc" android:layout_gravity="center" /> </FrameLayout>

RelativeLayout(相对布局)

相对布局,子控件的位置关系可以通过子控件与父控件、子控件与子控件来确定,子控件之间位置可以重叠,后面的控件会盖在前面控件之上,拓展性好,灵活方便,是使用最多的布局方式。

案例代码:

<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/et_uname" android:hint="请输入用户名" android:textSize="20sp" android:background="@drawable/corner_round" android:layout_alignParentTop="true" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:id="@+id/et_pwd" android:hint="请输入密码" android:inputType="textPassword" android:layout_marginTop="12dp" android:background="@drawable/corner_round" android:textSize="20sp" android:layout_below="@+id/et_uname" android:layout_width="match_parent" android:layout_height="wrap_content" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/btn_login" android:text="登录" android:layout_width="150dp" android:layout_height="wrap_content" /> <View android:id="@+id/v1" android:layout_toRightOf="@+id/btn_login" android:layout_width="50dp" android:layout_height="0dp" /> <Button android:id="@+id/btn_reg" android:layout_toRightOf="@+id/v1" android:text="注册" android:layout_width="150dp" android:layout_height="wrap_content" /> </RelativeLayout>

相对布局使用<RelativeLayout>标签,其常用属性如下:

android:layout_toLeftOf=”@+id/name” 指定控件的左边

android:layout_toRightOf=”@+id/name” 指定控件的右边

android:layout_above=”@+id/name” 指定控件的上边

android:layout_below=”@+id/name” 指定控件的下边

ndroid:layout_alignLeft=”@+id/name” 与指定控件左对齐

android:layout_alignRight=”@+id/name” 与指定控件右对齐

android:layout_alignTop=”@+id/name” 与指定控件顶部对齐


android:layout_alignBottom=”@+id/name” 与指定控件底部对齐


android:layout_alignParentLeft=”true” 与父控件的左边对齐


android:layout_alignParentRight=”true” 与父控件的右边对齐


android:layout_alignParentTop=”true” 与父控件顶部对齐


android:layout_alignParentBottom=”true” 与父控件底部对齐


android:layout_centerHorizontal=”true” 在父控件中水平居中


android:layout_centerVertical=”true” 在父控件中垂直居中


android_layout_centerInParent=”true” 在父控件中中部居中

AbsoluteLayout(绝对布局)

绝对布局,子控件的位置以绝对的位置定位,子控件之间可以重叠,相对于其他布局,缺少灵活性,在最新的android版本中已经不建议使用。

总结

在android布局控制中,最常用的是线性布局和相对布局,往往它们通常是配合使用,也就是嵌套使用。

关于layout_gravity与gravity的区别

从名字上可以看到,android:gravity是对元素本身说的,元素本身的文本显示在什么地方靠着换个属性设置,不过不设置默认是在左侧的。

android:layout_gravity是相对与它的父元素说的,说明元素显示在父元素的什么位置。

比如说button: android:layout_gravity 表示按钮在界面上的位置。 android:gravity表示button上的字在button上的位置。


路过

雷人

握手

鲜花

鸡蛋
精彩评论

沙发等你来坐!

发布文章

24小时热文

    返回顶部