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


相关推荐

  • python爱心代码制作_python如何编出一个爱心文字

    python爱心代码制作_python如何编出一个爱心文字[M][fts=2][ftc=F7941D]╭╮╭╮[/ft][ftc=F7941D]╭╮╭╮[/ft]\[ftc=FFF100]∨[/ft]/\[ftc=FFF100]∨[/ft]/([ftc=00BFF3]≥[/ft]﹏[ftc=00BFF3]≤[/ft])([ftc=EF6EA8]≥[/ft]﹏[ftc=EF6EA8]≤[/ft])/[ftc=…

    2022年10月18日
  • sublime3激活码-激活码分享

    (sublime3激活码)2021最新分享一个能用的的激活码出来,希望能帮到需要激活的朋友。目前这个是能用的,但是用的人多了之后也会失效,会不定时更新的,大家持续关注此网站~https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~ML…

    2022年3月21日
  • 数组转LIst的几种方法

    数组转LIst的几种方法第一种方式/***针对数组类型转换*分别是int[]、long[]、double[],其他数据类型比如short[]、byte[]、char[],在JDK1.8中暂不支持*/List<Integer>intList=Arrays.stream(newint[]{1,2,3,}).boxed().collect(Collectors.toList());List<Long>longList=Arrays.stream(newlong[]

    2022年6月21日
  • xmlns属性

    xmlns属性今天在看网上其他的项目,发现有一行代码我不明白意思:百度之后,才知道,这是一个属性。在XHTML中,xmlns属性是必需的。在HTML中,xmlns属性是无效的。xmlns属性规定文档的xml命名空间。然而,如果在XHTML文档中未使用xmlns, http://w3.org 的HTML验证器也能正确识别文档。这是因为命名空间”xmln

    2022年10月23日
  • vue取消eslint规范_vue运行eslint报错

    vue取消eslint规范_vue运行eslint报错关闭eslint代码规范检查

    2022年10月8日
  • java实现十进制转十六进制_十进制转十六进制java代码

    java实现十进制转十六进制_十进制转十六进制java代码基础练习十六进制转十进制时间限制:1.0s内存限制:512.0MB问题描述  从键盘输入一个不超过8位的正的十六进制数字符串,将它转换为正的十进制数后输出。注:十六进制数中的10~15分别用大写的英文字母A、B、C、D、E、F表示。样例输入FFFF样例输出65535太奇葩了,拿到这道题受上道题的影响,自己写了进制转化函数,结果,25分。。。。imp…

    2022年11月1日

发表回复

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

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