Android中的layout_gravity和gravity属性的区别

这两个属性说真从我开始接触Android的时候就开始傻傻的分不清,感觉有时候他们的作用是一样的,但有时候他们的作用又不一样。今天我们就搞搞他们有什么不一样的地方!

gravity:这是指文字控件中的对齐方式

  1. 当layout_width和layout_height都为包裹其自身内容时:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:gravity="center"
        />

</LinearLayout>

效果如下:
在这里插入图片描述

  1. 当layout_width为包裹其自身内容,layout_height跟随父容器的大小
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="Hello World!"
        android:gravity="center"
        />

</LinearLayout>

效果如下:
在这里插入图片描述

  1. 当layout_width跟随父容器的大小,layout_height为包裹其自身内容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:gravity="center"
        />

</LinearLayout>

效果如下:

在这里插入图片描述

  1. 当layout_width和layout_height都是跟随父容器的大小的时候
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Hello World!"
        android:gravity="center"
        />

</LinearLayout>

效果如下:
在这里插入图片描述

layout_gravity:这是指控件布局中中的对齐方式

值得注意的是:在LinearLayout中要是layout的排布方式是水平排布,那么layout_gravity的属性值只有在垂直方向才会生效,同理layout垂直方向的排布时layout_gravity也只有水平的属性值才会生效

  1. 当layout_gravity的值为空的时候
<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="horizontal"
    tools:context=".MainActivity">
    <!--android:textAllCaps="false" 这个属性式禁止将字母自动转换成大写字母-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1"
        android:textAllCaps="false"
        android:textSize="20sp"
        android:layout_gravity=""
        />


</LinearLayout>

效果如下:
在这里插入图片描述

2.由于LinearLayout的布局是horizontal,所以当layout_gravity中的布局为hcenter_vertical时没有生效

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="horizontal"
    tools:context=".MainActivity">
    <!--android:textAllCaps="false" 这个属性式禁止将字母自动转换成大写字母-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1"
        android:textAllCaps="false"
        android:textSize="20sp"
        android:layout_gravity="center_horizontal"
        />


</LinearLayout>

效果如下:
在这里插入图片描述

  1. 由于布局采用水平布局方式,水平方向上的空间式不确定的,所以在用layout_gravity的时候控件只会在原有的基础上进行水平或者式垂直平移(例子式垂值方向的平移)
<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="horizontal"
    tools:context=".MainActivity">
    <!--android:textAllCaps="false" 这个属性式禁止将字母自动转换成大写字母-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1"
        android:textAllCaps="false"
        android:textSize="20sp"
        android:layout_gravity="top"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button2"
        android:textAllCaps="false"
        android:textSize="20sp"
        android:layout_gravity="center_vertical"
        />


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button3"
        android:textAllCaps="false"
        android:textSize="20sp"
        android:layout_gravity="bottom"
        />


</LinearLayout>

效果如下:
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值