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


相关推荐

  • OPC协议_opc通讯协议简介

    OPC协议_opc通讯协议简介一、OPC:OPC是一种利用微软的COM/DCOM技术来达成自动化控制的协定,采用典型的C/S模式,针对硬件设备的驱动程序由硬件厂商完成,提供统一OPC接口标准的Server程序,软件厂商只需按照OPC标准接口编写Client程序就访问Server程序进行读写,即可实现与硬件设备的通信。OPC协定包括:1.DA(DataAccess)规范:访问数据主要采用该规范2.A&amp;E(Alarma…

    2025年8月23日
    5
  • navicat15.0.23 激活码(最新序列号破解)

    navicat15.0.23 激活码(最新序列号破解),https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月18日
    461
  • wptx64能卸载吗_Win10如何卸载应用?Win10内置应用卸载方法

    wptx64能卸载吗_Win10如何卸载应用?Win10内置应用卸载方法Win10如何卸载应用?Win10默认预装集成了很多应用,这是大多数用户都了解的共识,从个人角度上来看,其他还是很好看的,如果真的需要使用以下Win10内置应用卸载方法来处理,可能会让你失望的,毕竟TA们占用的资源并不多。Win10内置应用卸载方法:一、手动卸载法(针对可卸载的):1、卸载Win10应用最简单的方式就是在“开始”菜单上找到这些内置应用的动态磁铁,然后鼠标右击直接点击卸载即可。当…

    2022年5月2日
    393
  • C++中构造函数的作用「建议收藏」

    C++中构造函数的作用「建议收藏」构造函数用于解决类中的对象初始化的问题构造函数是一类特殊的函数,与其他的成员函数不同的是构造函数构造函数不需要用户来调用它,而是建立对象的时候自动的执行#include//#include”student.h”//#include//#includeusingnamespacestd;classTime{publi

    2025年8月29日
    5
  • 《学习笔记10》——JavaScript三目运算符的使用[通俗易懂]

    《学习笔记10》——JavaScript三目运算符的使用[通俗易懂]三目运算符是多种语言中,都有的一种语法,这里着重讲解JavaScript里的用法。1.判断基本语法:expression?sentence1:sentence2当expression的值为true时,执行sentence1,否则执行sentence2,请看如下代码:3>0?2:1等价于:if(3>0){return2}else{return1}意思是,当3>0成立时,返回2,否则返回1

    2022年6月22日
    34
  • MongoVUE_mongodb怎么用

    MongoVUE_mongodb怎么用一、查询位号名name以A05或者A08开头的值{$or:[{name:/A05.*/},{name:/A08.*/}]}二、查询时间范围

    2022年8月21日
    5

发表回复

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

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