gravity与layout_gravity的区别

gravity与layout_gravity的区别gravity与layout_gravity的区别

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

含义

gravity是相对于元素本身而言的,用于控制布局中控件的对齐方式,如果没有子控件,则表示内容的对齐方式。gravity常用值有top、bottom、left、right、center等,可以使用”|”将多个值连接起来。

layout_gravity是相对于父布局容器而言的,用于设置在父容器中的对齐方式。

例子

在LinearLayout中放置一个TextView,通过配置gravity和layout_gravity观察TextView出现的位置。
xml文件代码:在TextView中添加属性,android:layout_gravity=”center”

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/view1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_bright"
        android:layout_gravity="center"//设置TextView在父控件LinearLayout中居中
        android:text="@string/hello_world" />

</LinearLayout>
在LinearLayout中修改代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:gravity="center">//设置子控件TextView居中
    <TextView
        android:id="@+id/view1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_bright"
        android:layout_gravity="center"
        android:text="@string/hello_world" />

</LinearLayout>

gravity与layout_gravity的区别=
gravity与layout_gravity的区别


从上面的运行结果就可以看出,android:layout_gravity=”center”是让TextView显示在Layout的中间。
android:gravity=”center”则让LinearLayout中的子控件TextView居中。
再看一个例子

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/view1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_bright"
        android:gravity="center_horizontal"//添加代码的位置
        android:layout_gravity="center
        android:text="@string/hello_world" />

</LinearLayout>

gravity与layout_gravity的区别


TextView中没有子控件,那么它会对TextView中的内容起作用。字体在TextView中居中显示。


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

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

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


相关推荐

  • 电容分类和作用_电容的识别

    电容分类和作用_电容的识别硬件基础知识—电容分类智能硬件和物联网产品上,工作电压不高,其常用的电容根据不同的工艺,主要分为陶瓷电容、电解电容和钽电容。↑陶瓷电容的结构图↑电解电容的结构图不管是什么电容,都是两组金属片夹着一层介质。陶瓷电容把金属片交错摆放,电解电容把金属片卷成柱状。↑片状陶瓷电容↑贴片陶瓷电容陶瓷电容,MultilayerCeramicCapacitor(MLCC),陶瓷电容的电介质是陶瓷,所以叫做陶瓷电容。陶瓷电容容值小、电压高、尺寸小、高频性能好、不区分正.

    2022年8月22日
    5
  • Fastjson 对象或数组转JSON

    Fastjson 对象或数组转JSONFastjson对象或数组转JSONw3cshool:https://www.w3cschool.cn/fastjson/Fastjson对象或数组转JSON:https://www.w3cschool.cn/fastjson/fastjson-ex1.htmlFastjson阿里巴巴工程师开源的一个json库:Fastjson,这个库在解析速度和易用性上来说都很不错。在日志…

    2022年6月21日
    24
  • listagg小记录[通俗易懂]

    listagg小记录[通俗易懂]listagg的作用是将分组范围内的所有行特定列的记录加以合并成行。函数签名中的measure_expr为分组中每个列的表达式,而delimiter为合并分割符。如果delimiter不设置的话,就表示无分割符。  中间withingroup后面的order_by_clause表示的是进行合并中要遵守的排序顺序。而后面的over子句表明listagg是具有分析函数analyze

    2022年9月4日
    2
  • Fiddler+雷电模拟器进行APP抓包

    Fiddler+雷电模拟器进行APP抓包1、Fiddler下载地址:下载最新版Fiddler,强烈建议在官网下载:https://www.telerik.com/download/fiddler雷电模拟器下载地址:选择3.0正式版(注意,高版本无法抓包,只能下载3.0正式版)https://www.ldmnq.com/other/version-history-and-release-notes.html?log=3正常傻瓜式安装,下一步,下一步,安装完毕后,先不用急于打开软件。3.下载并安装Fiddler证书生成器:http:

    2022年5月30日
    196
  • monty python喜剧-看美剧学托福:最受美国人喜欢的100部美剧「建议收藏」

    monty python喜剧-看美剧学托福:最受美国人喜欢的100部美剧「建议收藏」在之前做了一个百大电影的清单之后,好莱坞知名媒体THR再次发布年度重量级策划,他们邀请了2800多名好莱坞影视从业人员,包括779名演员,365名制片人,268名导演等等,由他们选出自己最爱的剧集(主要是美剧,也有一些其他的秀),最终汇总成为这个百大剧集清单,你放心,这里面,一定有你超爱的。以及,第一,果然是那部,当之无愧的热门剧。100、绝望主妇DesperateHousewives(200…

    2022年9月30日
    0
  • Yum源配置_linux配置离线yum源

    Yum源配置_linux配置离线yum源使用说明Euler的镜像地址为:https://mirrors.huaweicloud.com/euler/1、在/etc/yum.repos.d/目录下,创建文件EulerOS.repo,根据EulerOS版本及系统架构选择配置内容。EulerOS版本及系统架构:[base]name=EulerOS-2.0SP8basebaseurl=http://mirrors….

    2022年8月12日
    6

发表回复

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

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