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

有哪些布局类型?Android系统中为我们提供的五大布局:LinearLayout(线性布局)、FrameLayout(单帧布局)、AbsoluteLayout(绝对布局)、TablelLayout(表格布局)、RelativeLayout(相对布局

有哪些布局类型?

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上的位置。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 55@qq.com 举报,一经查实,本站将立刻删除。转转请注明出处:https://www.szhjjp.com/n/32180.html

(0)
新劵
上一篇 2021-10-01
下一篇 2021-10-01

相关推荐

  • 申请低保的条件和程序了解(申请低保的条件和程序)

    一、申请条件1.持有本地常住户口的居民2.共同生活的家庭成员人均收入低于本地低保标准3.家庭财产状况和其他情况符合条件的对象。二、申请材料1.身份证、户口本复印件;2.信息核对授权书;3.家庭经济状况申明及承诺书;4.最低生活保障申请书;5

    2021-12-12
    0
  • u启动u盘做系统详细步骤图解(用u盘启动盘重装系统)

    u启动是一款操作便携的u盘启动盘制作工具,其强大的兼容性能够兼容市面上绝大多数的电脑配置,而且安装系统的过程都相当简练,就算是电脑小白也能够根据u启动官网提供的操作步骤独立完成整个系统安装过程,下面为大家介绍u启动u盘启动盘制作工具制作u

    2021-12-11 科技
    0
  • win7怎样给电脑设置ip地址(ip地址设置win7)

    电脑的IP地址原本是自动获取的,由于不固定性,不是很方便,今天在这里教大家如何设置win7台式电脑的IP地址为固定IP地址。1、在设置电脑IP之前我们首先要知道现在的IP地址以及DNS服务器地址是什么,我们点击电脑桌面左下角的开始菜单,

    2021-12-11 科技
    0
  • Linux系统目录结构(linux目录操作命令)

    登录系统后,在当前命令窗口下输入命令:ls/你会看到如下图所示:树状目录结构:以下是对这些目录的解释:/bin:bin是Binaries(二进制文件)的缩写,这个目录存放着最经常使用的命令。/boot:这里存放

    2021-12-11
    0
  • 教你win7兼容性如何设置(win7兼容性设置在哪里)

    win7运行速度快,操作便捷性与XP系统是一样简单,界面也比较清爽,兼容性也是微软系统中的王者。不过也有个别的软件不能运行,那么我们如何设置呢?今天,我就给大家介绍一下设置win7兼容性的小技巧。1、右击有问题的软件的图标(注意不是快捷

    2021-12-11 科技
    0
  • 家里的蜈蚣是哪里爬出来的(蜈蚣进家里怎么找到它)

    家里的蜈蚣是哪里爬出来的是时下很多住在高层的朋友都有的疑问,毕竟在很多人的认知里觉得楼层高家里便不会出现蟑螂、蜈蚣这些东西,其实这种想法是错误的,只要你家中有适合蜈蚣生存的环境,那么在家中就一定会看到它。家里的蜈蚣是哪里

    2021-09-06
    0

发表回复

登录后才能评论