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


相关推荐

  • 怎样重装系统win10(开机进不了windows系统)

    超级简单的方法重装win10系统重装系统操作步骤如果电脑系统还可以进入,那就没必要做U盘启动项,直接在现有的系统里重装win10。如果你的系统坏了,进不去了,这时你就要用U盘作为启动项安装系统。到Microsoft官网下载安装工具,用这个工具安装win10系统非常方便。利用此工具不仅可以在原有系统重装win10,也可以用来做U盘启动项。链接:link.重装系统操作步骤如果电脑系统还可以…

    2022年4月12日
    111
  • JAVASCRIPT 上传文件的几种方式「建议收藏」

    JAVASCRIPT 上传文件的几种方式「建议收藏」方法1:使用ajax,通过formdata传参//注意:FormData只兼容到IE10varformData=newFormData();varfile=$(‘.import-file-btn’).get(0).files[0];formData.append(‘file’,file);formData.append(‘id’,id);formData.append(‘name’,name);$.ajax({url:’user/validate_impo

    2025年8月15日
    3
  • 纯净版系统怎么安装_纯净版安装版win7

    纯净版系统怎么安装_纯净版安装版win7百度网盘下载:1.链接:https://pan.baidu.com/s/1o-HcKddSG6IAz_0COKhq8Q提取码:hkhr2.扫码下载:

    2022年8月4日
    6
  • Git 取消跟踪已版本控制的文件(亲测可行)

    Git 取消跟踪已版本控制的文件(亲测可行)

    2021年10月20日
    58
  • 稀疏数组的应用

    稀疏数组的应用稀疏数组一 稀疏数值二 使用步骤 1 引入库 2 读入数据总结一 稀疏数值当一个数组中大部分元素为 0 时 或者为同一个值的数组时 可以使用稀疏数组来保存该数组 二 使用步骤 1 引入库代码如下 示例 importnumpya pyplotasplti filterwarnin ignore import

    2025年11月18日
    4
  • GoogleNet_我和我的祖国论文摘要

    GoogleNet_我和我的祖国论文摘要GoogLeNetIncepetionV1这是GoogLeNet的最早版本,出现在2014年的《Goingdeeperwithconvolutions》。之所以名为“GoogLeNet”而非“GoogleNet”,文章说是为了向早期的LeNet致敬。Motivation深度学习以及神经网络快速发展,人们不再只关注更给力的硬件、更大的数据集、更大的模型,而是更在意新的idea、新的算法以及模型的改进。一般来说,提升网络性能最直接的办法就是增加网络深度和宽度,即增加隐层和以及各层神经元

    2022年8月14日
    4

发表回复

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

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