Android界面布局属性layout_gravity和gravity的区别

Android界面布局属性layout_gravity和gravity的区别一、layout_gravity和gravity的作用1、android:layout_gravity是设置该控件相对于父容器对齐方式;2、android:gravity是设置子元素在该容器内的对齐方式。3、layout_gravity和gravity可以设置的值:top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical。(一个属性可以包

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

一、layout_gravity和gravity的作用

1、android:layout_gravity是设置该控件相对于父容器对齐方式;
2、android:gravity是设置子元素在该容器内的对齐方式。
3、layout_gravity和gravity可以设置的值:top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical。(一个属性可以包含多个值,需用 “|” 分开),其具体作用如下:
在这里插入图片描述
二、layout_gravity和gravity在各布局中的使用区别

1、相对布局(RelativeLayout)

<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" tools:context=".MainActivity">

    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="Hello Android!"/>
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Hello World!"/>
</RelativeLayout>

效果图如下:
在这里插入图片描述
在相对布局中layout_gravity和gravity不起任何作用

2、线性布局(LinearLayout )
(1)当我们采用垂直排列( android:orientation=“vertical” )时,

<LinearLayout 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:orientation="vertical" tools:context=".MainActivity">

    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Hello Android!"/>

</LinearLayout>

效果图如下:
在这里插入图片描述

符合我们预期的效果,当我们将 android:layout_gravity=“center_horizontal” 改为 android:gravity=“center_horizontal” 时,其效果图如下:
在这里插入图片描述
不符合我们预期的效果,没有起到任何作用。
(2)当我们采用水平布局( android:orientation = “horizontal” )时,

<LinearLayout 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:orientation="horizontal" tools:context=".MainActivity">

    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="Hello Android!"/>

</LinearLayout>

效果图如下:
在这里插入图片描述
符合我们预期的效果,当我们将 android:layout_gravity=“center_vertical” 改为 android:gravity=“center_vertical” 时,其效果图如下:
在这里插入图片描述
不符合我们预期的效果,没有起到任何作用。
故我们在线性布局中使用layout_gravity和gravity应该注意以下几点:

1、gravity在线性布局中不起任何作用,layout_gravity在线性布局中起作用;
2、 当我们使用 android:orientation=“vertical” 时, android:layout_gravity只有水平方向的设置才起作用,
垂直方向的设置不起作用。即:left,right,center_horizontal 是生效的;
3、当 我们使用android:orientation=“horizontal” 时, android:layout_gravity只有垂直方向的设置才起作用,
水平方向的设置不起作用。即:top,bottom,center_vertical 是生效的。

初学Android,自我感觉对Android界面布局属性layout_gravity和gravity认识还不够深,若有错误欢迎赐教。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/158761.html原文链接:https://javaforall.net

(0)
上一篇 2022年7月26日 下午7:16
下一篇 2022年7月26日 下午7:36


相关推荐

  • springboot+idea热部署(自动刷新)

    springboot+idea热部署(自动刷新)近来在使用idea做springboot的项目,但是发现每次修改之后我都需要重新将项目关闭再开启,这样比较繁琐,发现通过热部署的方式让我们可以一边修改我们的项目,然后在页面中直接通过刷新展示出来spring为开发者提供了一个名为spring-boot-devtools的模块来使SpringBoot应用支持热部署,提高开发者的开发效率,无需手动重启SpringBoot应用。devtool…

    2022年6月26日
    50
  • st7789 旋转_玩转 ESP32 + Arduino(二十八) TFT_eSPI库驱动ST7789

    我们用到的库TFT_eSPI一.硬件接线这里我们使用了中景园的ST7789一般屏幕的引脚定义如下:接线:我们直接用VSPI接线ESP32引脚ST7789引脚功能GNDGND接地3V3VCC电源(VCLK)18SCLSPI时钟线(VMOSI)23SDASPI主出从入线26RES复位引脚27DC数据/命令选择线(VCS0)5CSSPI片选线没接BLK背光控制线如何在TFT_eSPI中设置引脚??…

    2022年4月9日
    1.1K
  • Pycharm 搜索功能大全「建议收藏」

    Pycharm 搜索功能大全「建议收藏」目录Ctrl+N按文件名搜索py文件Ctrl+shift+N按文件名搜索所有类型的文件ctrl+shift+f全局字符串搜索ctrl+shift+a双shift搜索Ctrl+N按文件名搜索py文件ctrl+n可以搜索py文件勾选上面这个框可以搜索工程以外的文件Ctrl+shift+N按文件名搜索所有类型的文件Ctrl+shift+N可以搜索…

    2022年8月26日
    9
  • linux发起iscsi_iscsi自动连接

    linux发起iscsi_iscsi自动连接1、存储介质1)磁盘阵列:磁盘阵列是一种采用RAID技术、冗余技术和在线维护技术制造的一种高性能、高可用的磁盘存储设备。2)IP-SAN存储:SAN(StorageAreaNetwork-存储区域网络):是计算机信息处理技术中的一种架构,它将服务器和远程的计算机存储设备(如磁盘阵列、磁带库)连接起来,使得这些存储设备看起来就像是本地一样。SAN就理解成存储虚拟化,而IP-SAN就是采

    2022年8月23日
    9
  • JS后退一页

    JS后退一页Javascript返回上一页:1.history.go(-1),返回两个页面:history.go(-2);2.history.back().3.window.history.forward()返回下一页4.window.history.go(返回第几页,也可以使用访问过的URL)例:向上一页response.Write(“”) response.W

    2022年7月25日
    16
  • Delphi中QuotedStr介绍及使用

    Delphi中QuotedStr介绍及使用delphi函数给字符串两边加单引号并返回.声明:functionQuotedStr(constS:string):string;用函数QuotedStr把字符串S转换成为用引号括起来的字符串。单引号”‘”将被插入到字符串s的最前和最后。例如:abc->’abc’

    2022年10月18日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号