Android中的layout_gravity和gravity的区别[通俗易懂]

Android中的layout_gravity和gravity的区别[通俗易懂]在Android的布局中,除了padding和margin容易弄混之外,还有layout_gravity和gravity。按照字面意思来说,layout_gravity就是相对于layout来设置的。

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

在Android的布局中,除了padding和margin容易弄混之外,还有layout_gravity和gravity。按照字面意思来说,layout_gravity就是相对于layout来设置的。通俗点讲就是当前控件之外的位置。

而gravity就是控件内部的位置。具体来看一下Demo。

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:orientation=”vertical” >
    
    <TextView
        android:background=”@android:color/holo_blue_light”
        android:textColor=”@android:color/black”
        android:layout_width=”500dp”
        android:layout_height=”100dp”
        android:text=”@string/test”/>

</LinearLayout>

 这个是原始的layout。没有设置layout_gravity和gravity

Android中的layout_gravity和gravity的区别[通俗易懂]

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:layout_gravity=”center”
    android:orientation=”vertical” >
    
    <TextView
        android:background=”@android:color/holo_blue_light”
        android:textColor=”@android:color/black”
        android:layout_width=”500dp”
        android:layout_height=”100dp”
        android:text=”@string/test”/>

</LinearLayout>

此时,我们将LinearLayout设置一个layout_gravity。发现没有什么作用,生成的效果还是跟上图一样。然后我们回到前面讲的,是相对于该控件之外的布局。

但是我们的LinearLayout的width和height已经是match_parent了,已经占满整个屏幕了,所以再居中的话,是没有任何效果的。好,我们现在将width和height改成wrap_content看下效果。

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
    android:layout_width=”wrap_content”
    android:layout_height=”wrap_content”
    android:layout_gravity=”center”
    android:orientation=”vertical” >
    
    <TextView
        android:background=”@android:color/holo_blue_light”
        android:textColor=”@android:color/black”
        android:layout_width=”500dp”
        android:layout_height=”100dp”
        android:text=”@string/test”/>

</LinearLayout>

Android中的layout_gravity和gravity的区别[通俗易懂]

发现,整个layout已经居中了。因为我们设置的width和height是wrap_content,所以layout的大小就是TextView的大小。而外面黑色部分就是andoroid的rootView。这里不做详细说明。

然后我们先将layout的width和height设置为match_parent。看看gravity有什么作用。

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:gravity=”center”
    android:orientation=”vertical” >
    
    <TextView
        android:background=”@android:color/holo_blue_light”
        android:textColor=”@android:color/black”
        android:layout_width=”500dp”
        android:layout_height=”100dp”
        android:text=”@string/test”/>

</LinearLayout>

发现效果跟上图的效果一样。Layout里面的TextView也是居中的。所以就知道,gravity是相对于控件内部居中的。

我们再整合一下,放到TextView里面。看看效果。

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:orientation=”vertical” >
    
    <TextView
        android:background=”@android:color/holo_blue_light”
        android:textColor=”@android:color/black”
        android:layout_gravity=”center”
        android:gravity=”center”
        android:layout_width=”500dp”
        android:layout_height=”100dp”
        android:text=”@string/test”/>

</LinearLayout>

 

 Android中的layout_gravity和gravity的区别[通俗易懂]

发现TextView里面的文字居中了(gravity起的作用)。TextView的位置相对于LinearLayout也居中了(layout_gravity起的作用)。

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

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/154449.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • Mac PHPStorm清除SVN配置缓存

    Mac PHPStorm清除SVN配置缓存

    2021年10月21日
  • 必知必会 RabbitMQ面试题 33道(附答案)「建议收藏」

    必知必会 RabbitMQ面试题 33道(附答案)「建议收藏」点击关注公众号,回复000获取优质资料前言大家好,我是老田。今天我们来分享RabbitMQ消息队列。其中,MQ(MessageQueue)翻译过来就是消息队列的意思。RabbitMQ…

  • ed2k链接网站

    ed2k链接网站 http://ed2k.shortypower.org/  查源站 http://donkey4u.com/   查源站 http://verycd.gdajie.com/ http://www.iverycd.com/ http://www.qvocd.org/ http://www.simplecd.me/ http://www.ed2kers.com/ http://www.icili….

  • 一句话木马怎么连接_js木马源码

    一句话木马怎么连接_js木马源码“EASYNEWS新闻管理系统v1.01正式版”是在企业网站中非常常见的一套整站模版,在该网站系统的留言本组件中就存在着数据过滤不严漏洞,如果网站是默认路径和默认文件名安装的话,入侵者可以利用该漏洞直接上传ASP木马程序控制整个网站服务器。Step1搜索入侵目标使用了“EASYNEWS新闻管理系统v1.01正式版”的网站,在网站页面的底部版权声明处,往往会有关键字符为“WWW.52EAS…

    2022年10月30日
  • 玩转Redis-京东签到领京豆如何实现

    玩转Redis-京东签到领京豆如何实现-京东签到日历的产品逻辑是怎样的?-传统关系型数据库该如何实现?-表设计初级玩法(**80%的人只会这么玩**)-表设计进阶玩法(**高级程序员才会的玩法**)-查询签到情况及签到的技术实现-基于Redis的Bitmaps实现签到日历(**瞬间提升档次**)-什么是Bitmaps-Bitmaps如何使用(含详细命令对比分析及示例)-BitMap实战签到日历-业务总结/技术总结

  • 【007】【JVM——内存分配和恢复策略】

    【007】【JVM——内存分配和恢复策略】

发表回复

您的电子邮箱地址不会被公开。

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