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

相关推荐

  • linux vim dd命令_vim命令和vi的区别

    linux vim dd命令_vim命令和vi的区别Vim是从vi发展而来的文本编辑器,可以用颜色或底线等方式来显示一些特殊的信息。Vim是Linux中必不可少的工具,搭建网站修改配置文件时经常用到。本教程介绍Vim的模式和常用操作。背景信息Vim的各个模式介绍如下表所示:模式作用模式转换普通模式(NormalMode)在该模式下,您可以复制、粘贴、删除字符或行。运行vim打开文件时,即进入普通模式。在其他四个模式下,按Esc键…

    2022年9月22日
  • python究竟要不要使用多线程

    (1)python多线程究竟有没有用?(2)python虚拟机机制如何控制代码的执行?(3)python中多进程处理原理是怎么样的?1.先来看两个例子(1)例1分别用单线程、使用多线程、使

    2021年12月29日
  • 面试手写代码的经验分享

    面试手写代码的经验分享前言今天分享一下自己面试的时候遇到的写面试题的经验分享。在面试的时候,分为视频面试和现场面试,接下来讲解着两部分。视频面试在我和我周围的同学参加过的公司里面,不在现场的面…

    2022年5月16日
  • unity调用animation_unity随机地图

    unity调用animation_unity随机地图Unity小科普老规矩,先介绍一下Unity的科普小知识:Unity是实时3D互动内容创作和运营平台。包括游戏开发、美术、建筑、汽车设计、影视在内的所有创作者,借助Unity将创意变成现实。Unity平台提供一整套完善的软件解决方案,可用于创作、运营和变现任何实时互动的2D和3D内容,支持平台包括手机、平板电脑、PC、游戏主机、增强现实和虚拟现实设备。也可以简单把Unity理解为一个游戏引擎,可以用来专业制作游戏!Unity踩坑小知识点学习Unity使用Q..

    2022年9月1日
  • Jlink接口引脚定义「建议收藏」

    Jlink接口引脚定义「建议收藏」做东西时经过上网找引脚图接线,mark一下,以备后用:

    2022年5月28日
  • cube-ui的用法

    cube-ui的用法1.安装:npminstallcube-ui-S2.修改.babelrc:(添加到plugins中去){"plugins":[["transform-m

    2022年8月3日

发表回复

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

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