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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 数据结构–(ElemType *&T)代表的意义「建议收藏」

    数据结构–(ElemType *&T)代表的意义「建议收藏」1、前言ElemType表示抽象数据类型。首先看个例子:函数1:voidswap1(intx,inty){inttemp;temp=x;x=y;y=temp;}函数2:voidswap2(int&x,int&y){inttemp;temp=x;x=y;…

    2022年5月19日
    55
  • QT 面试题汇总[通俗易懂]

    QT 面试题汇总[通俗易懂]一、讲述Qt信号槽机制与优势与不足优点:①类型安全。需要关联的信号槽的签名必须是等同的。即信号的参数类型和参数个数同接受该信号的槽的参数类型和参数个数相同。若信号和槽签名不一致,编译器会报错。②松散耦合。信号和槽机制减弱了Qt对象的耦合度。激发信号的Qt对象无需知道是那个对象的那个信号槽接收它发出的信号,它只需在适当的时间发送适当的信号即可,而不需要关心是否被接受和那个对象接受了。Qt就保证了适当的槽得到了调用,即使关联的对象在运行时被删除。程序也不会奔溃。…

    2022年6月25日
    149
  • Python,PyCharm,Anaconda三者间的关系[通俗易懂]

    Python,PyCharm,Anaconda三者间的关系[通俗易懂]1.Python:解释性脚本语言,提供人机对话的解释工作。2.PyCharm:用来写Python代码的工具,是一种集成开发环境(IDE)。在这种环境下可进行可视化地代码程序编写工作。3.Anaconda:Python自身缺少一些包(如:NumPy,Scipy),需要在cmd终端输入pipinstallnumpy安装包。Anaconda内置了许多第三方库,是一个基于Python的数据处理和科学计算平台。…

    2022年8月27日
    2
  • atm异步传输模式特性_ATM是什么模式

    atm异步传输模式特性_ATM是什么模式AsynchronousTransferMode.  ATM是一种传输模式,在这一模式中,信息被组织成信元,因包含来自某用户信息的各个信元不需要周期性出现,这种传输模式是异步的。   ATM是网络新技术,它采用基于信元的异步传输模式和虚电路结构,根本上解决了多媒体的实时性及带宽问题。实现面向虚链路的点到点传输,它通常提供155Mbps的带宽。它既汲取了话务通讯中电路交换的“有连接”服务

    2022年9月21日
    0
  • linux bash 漏洞 利用,Linux Bash发现重大漏洞

    linux bash 漏洞 利用,Linux Bash发现重大漏洞LinuxBash发现重大漏洞Posted:26.09.2014|Author:QuenyWell|Category:LinuxGNUBourneAgainShell(Bash)是Linux和Unix系统中广泛使用的命令行Shell。最近发现的bash重大漏洞允许攻击者运行任意代码,从而接管系统。由于Bash使用广泛,而此漏洞的利用难度极低,可以预见近期将有大量基于此漏洞…

    2022年10月30日
    0
  • 网站管理后台帐号密码暴力激活成功教程方法

    网站管理后台帐号密码暴力激活成功教程方法【导读】对于网站运行的个人站长而言,最担心的是应如何有效且安全的去管理自己的网站,否则自己辛辛苦苦经营的网站就会被不请自来的不速之客给攻破,轻则站点数据被窃取,重则整个网站都被攻陷,导致无法恢复。本文主要从管理后台这个方面来讲解其黑客攻击过程,并通过在虚拟环境中展开实例演示,各读者可以跟着本教程去做实验,通过实验加强对攻击过程的了解,如果你是一名菜鸟站长也可以针对性的去做一下防护方案。…

    2022年10月13日
    0

发表回复

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

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