layout_gravity和gravity 关于属性失效问题

本文深入解析Android布局属性layout_gravity与gravity的区别,并通过实例演示它们在不同场景下的应用与限制,帮助开发者理解如何正确使用这些属性以达到预期的布局效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

相信对于Android的初学者来说,大家都曾经被layout里这两个极其相似的属性迷惑过。

简单使用一下搜索工具,我们就不难找到下面这样的答案:

layout_gravity 表示组件自身在父组件中的位置 
gravity             表示组件的子组件在组件中的位置

看似很简单嘛~)

貌似大伙瞅一眼就明白了。今天我要说的就是这貌似瞅一眼就明白的道理。 
为什么这么简单的道理,总有同学会发现,在“某些时候”,layout_gravity这个属性不好使了,失去了它应有的作用。

于是同学们又开始使用搜索工具,一边还不停的骂:Google做的这个layout真他妈的不好使! 
下面我们就网上找来的一个例子来简单描述一种layout_gravity失效的情况。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- android:gravity设置了按钮上面的文字的显示位置,而android:layout_gravity设置了按钮在布局中的显示位置。 –>
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.         android:orientation="vertical"
  5.         android:layout_width="fill_parent"
  6.         android:layout_height="wrap_content">
  7.         <Button android:layout_width="250dip"
  8.                 android:gravity="right"
  9.                 android:layout_height="wrap_content"
  10.                 android:text="我居右显示"
  11.                 android:layout_gravity="right" />;
  12. </LinearLayout>
复制代码

这段代码的展现效果如下:!

 

 

然后我们再看下面这段代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- android:gravity设置了按钮上面的文字的显示位置,而android:layout_gravity设置了按钮在布局中的显示位置。 –>
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.         android:layout_width="fill_parent"
  5.         android:layout_height="wrap_content">
  6.         <Button android:layout_width="250dip"
  7.                 android:gravity="right"
  8.                 android:layout_height="wrap_content"
  9.                 android:text="我居右显示"
  10.                 android:layout_gravity="right" />
  11. </LinearLayout>
复制代码

这段代码我们一样设置了android:layout_gravity="right",但是它的展现效果却是下面这样的:

 

 

于是我们前面所说的情况就发生了,“Google做的这个layout真他妈的不好使!”

问题究竟出在哪里了呢? 
细心一点的同学就会发现,下面的这段代码,最外层的LinearLayout少了这样一个属性:androidrientation="vertical" 
不错,正是缺少了这个属性才导致了android:layout_gravity="right"的失效。 
因为LinearLayout默认的是:androidrientation="horizontal"

也就是说,只有在作为父layout的LinearLayout是androidrientation="vertical" 的时候,android:layout_gravity="right"才会生效。

到这里本次“讲座”可以结束了吗? No!

看完上面的内容,有同学也许还会发现,当外层的LinearLayout为androidrientation="vertical" 的时候,android:layout_gravity="bottom"失效了

看到这里相信大家都明白了

下面我们还需要做一个简单的总结: 
当作为父layout的LinearLayout的属性为androidrientation="vertical" 的时候,android:layout_gravity="?"这里设为横向的时候才能生效。比如:left,right,center_horizontal等

当作为父layout的LinearLayout的属性为androidrientation="horizental" 的时候,android:layout_gravity="?"这里设为纵向的时候才能生效。比如:top,bottom,center_vertical等;

有一个比较特殊的是center,不管是横向还是纵向的时候,它总有一个方向起作用

另外有几个别急面生的:fill_vertical,fill_horizontal,fill,clip_vertical,clip_horizontal 
这几个以前没留意过,莫非是新版本的SDK中,新引进的?分散对齐?很迷惑。 
有用过的同学还请不吝赐教一下

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ModbusConnectActivity"> <!-- 添加一个 LinearLayout 来放置照片、显示框按钮 --> <LinearLayout android:id="@+id/topLayout" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:padding="16dp" android:layout_marginBottom="8dp"> <!-- 用于与下面的 FrameLayout 保持间隔 --> <!-- 照片显示框 --> <ImageView android:id="@+id/photoImageView" android:layout_width="100dp" android:layout_height="50dp" android:src="@drawable/tu2" android:contentDescription="Photo" android:layout_marginEnd="16dp" /> <!-- 显示框 --> <TextView android:id="@+id/displayTextView" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Your Text Here" android:textSize="16sp" android:gravity="center_vertical" android:paddingEnd="16dp" /> <!-- 按钮 --> <Button android:id="@+id/actionButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="获取TCU地址" android:onClick="onButtonClick" /> <!-- 自定义按钮点击事件 --> </LinearLayout> <FrameLayout android:id="@+id/content" android:layout_above="@id/bottomNavigationView" android:layout_width="match_parent" android:background="@drawable/bg" android:layout_height="match_parent"/> <!-- 底部导航栏 --> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottomNavigationView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" app:menu="@menu/navigation" app:labelVisibilityMode="labeled"/> </RelativeLayout>将LinearLayout与FrameLayout隔开
最新发布
03-11
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值