详解Android布局中gravity与layout_gravity属性[通俗易懂]

详解Android布局中gravity与layout_gravity属性[通俗易懂]在android布局中,我们经常会用到“重心”-gravity这个属性。但是gravity有不同的类型:gravitylayout_gravity相对布局中的layout_center等属性今天我们就来具体说说。1、gravitygravity属性是对控件自身内容对自己的限定,拿布局文件test.xml举例来说:此时在TextView中并没有对gravity属性进行操作,文字内容如上图。接下

大家好,又见面了,我是你们的朋友全栈君。

在android布局中,我们经常会用到“重心”-gravity这个属性。但是gravity有不同的类型:

  • gravity
  • layout_gravity
  • 相对布局中的layout_center等属性

今天我们就来具体说说。

1、gravity

gravity属性是对控件自身内容对自己的限定,拿布局文件test.xml举例来说:

这里写图片描述

此时在TextView中并没有对gravity属性进行操作,文字内容如上图。接下来,我们继续设置TextView的gravity属性,观察效果:

这里写图片描述

2、layout_gravity属性

与gravity属性不同的是,layout_gravity属性是用来设置该View相对与父View的位置,具体情况就个人判断有下面这4种情况:

这里写图片描述

另外还有一种在父布局横或纵设置wrap_content时,如果在该方向设置layout_gravity属性。我直接在一个布局中,把这5种情况列出来,下面是我的布局文件代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.primexiao.myapplication.MainActivity" >
//第4种情况
<LinearLayout  android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="#00f0f0" android:orientation="vertical">
        <TextView  android:id="@+id/tv1" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_centerInParent="true" android:text="我是测试内容" android:background="#000000" android:layout_gravity="center_horizontal" android:textColor="#ffffff" android:layout_width="100dp" android:layout_height="100dp" />
</LinearLayout>
//第1种情况
    <LinearLayout  android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="#00ff00" android:orientation="vertical">
        <TextView  android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_centerInParent="true" android:text="我是测试内容" android:background="#000000" android:textColor="#ffffff" android:layout_gravity="center_vertical" android:layout_width="100dp" android:layout_height="100dp" />
    </LinearLayout>
    //第3种情况
    <LinearLayout  android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="#00f0f0" android:orientation="horizontal">
        <TextView  android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_centerInParent="true" android:text="我是测试内容" android:background="#000000" android:layout_gravity="center_vertical" android:textColor="#ffffff" android:layout_width="100dp" android:layout_height="100dp" />
    </LinearLayout>
    //第2种情况
    <LinearLayout  android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="#ff0000" android:orientation="horizontal">
        <TextView  android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_centerInParent="true" android:text="我是测试内容" android:background="#000000" android:layout_gravity="center_horizontal" android:textColor="#ffffff" android:layout_width="100dp" android:layout_height="100dp" />
    </LinearLayout>
    //第5种情况
    <LinearLayout  android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" android:background="#fff000" android:orientation="horizontal">
        <TextView  android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_centerInParent="true" android:text="我是测试内容" android:background="#000000" android:layout_gravity="center_horizontal" android:textColor="#ffffff" android:layout_width="100dp" android:layout_height="100dp" />
    </LinearLayout>
</LinearLayout>

效果图如下:

这里写图片描述

我们可以看到第1和第2种情况下,layout_gravity这一属性根本没有起到作用,个人看法是子控件如果选择横或纵居中,这种属性声明是不能和父布局的排列方式相冲的,这个坑我先替你们踩着:D。

3、相对布局中的layout_center属性

之前遇到过这么一个问题,在RelativeLayout中设置layount_gravity属性,发现并不能实现居中效果,并且layout_gravity也是手动输入,期间并没有智能提示。后来发现相对布局中,有layout_centerX这么一个属性,让我们来试一下:

这里写图片描述

待续。

如果觉得这篇文章对你有帮助的话,欢迎顶+评论:D

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

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • log库spdlog简介及使用[通俗易懂]

    log库spdlog简介及使用[通俗易懂]spdlog是一个开源的、快速的、仅有头文件的C++11日志库,code地址在https://github.com/gabime/spdlog,目前最新的发布版本为0.14.0。它提供了向流、标准输出、文件、系统日志、调试器等目标输出日志的能力。它支持的平台包括Windows、Linux、Mac、Android。spdlog特性:(1)、非常快,性能是它的主要目标;(2)、仅包括…

    2022年6月23日
    52
  • 电子信息系统机房设计规范 GB50174-2017

    电子信息系统机房设计规范 GB50174-2017一、物理安全1.1物理安全主要包括:(1)机房环境安全(2)通信线路安全(3)设备安全(4)电源安全1.1.1机房的安全等级分为三个基本类别:A类:对计算机机房的安全有严格的要求,有完

    2022年7月2日
    32
  • Linux ab 压力测试

    Linux ab 压力测试

    2022年2月14日
    41
  • python换行符号怎么用_python中回车用什么表示

    python换行符号怎么用_python中回车用什么表示1、python语句,一般使用换行分隔,也就是说一行一条语句,一行过长的语句可以使用反斜杠(\)分解成几行小栗子#-*-coding:utf-8-*-#!/usr/bin/envpythona=102if(a==102)and\(a!=0):printa2、当然,不使用反斜线也可以跨行,那么就是用传说中的“三引号:(”””)”小栗子(如果打印的内容需要分成多行…

    2022年10月7日
    2
  • C语言中%c与%s的区别与划分「建议收藏」

    C语言中%c与%s的区别与划分「建议收藏」%c格式对应的是单个字符,%s格式对应的是字符串。例:chara;charb[20];scanf("%c",&amp;a);//只能输入一个字符。scanf("%s",b);//可以输入一串不超过20字符的字符串。%c对应类型为char,%s对应类型为char,即字符串.用作输入时,二者参数都要传char型.%c输入函数只会对一个字节空间赋值.而%s会一直赋值,直到输入中遇…

    2022年5月13日
    113
  • Java编译时类型和运行时类型「建议收藏」

    Java编译时类型和运行时类型「建议收藏」一、前言最近在做笔试题的时候,才看到有这么一个知识点,查了好几篇博客,在这里记录一下二、是什么Java引用变量有两个类型,一个是编译时类型,还有一个是运行时类型。编译时类型是由声明该变量时使用的类型所决定,运行时类型是由该变量指向的对象类型决定如果两种类型不一致,就会出现多态,因此就会将子类对象之间赋值给父类引用变量,称为向上转型,而不用进行类型转换。如Animal…

    2022年5月29日
    51

发表回复

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

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