今天在一个布局文件中,遇到了一个问题,先看代码

 <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="21dip"
android:paddingLeft="@dimen/setup_fragment_padding_left"
android:paddingRight="@dimen/setup_fragment_padding_right" > <!-- Buttons below -->
<!--
In order to show these buttons above the IME keyboard, we need to special case the to
padding to a smaller height.
-->
<Button
android:id="@+id/manual_setup"
style="@style/accountSetupButtonVfive"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/button_margin_left"
android:background="@drawable/email_btn_set"
android:text="@string/account_setup_basics_manual_setup_action" /> <Button
android:id="@+id/next"
style="@style/accountSetupButtonVfive"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="@drawable/email_btn_next"
android:layout_marginRight="@dimen/button_margin_right"
android:text="@string/next_action" />
</LinearLayout>

上述代码的目的,就是让两个按钮,一个靠左边,一个靠右边。我增加了一个属性

<android:layout_gravity="right">

结果发现一直不起作用。后来在网上查到了相关的解释

如下

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

看似很简单嘛

为什么这么简单的道理,总有同学会发现,在“某些时候”,layout_gravity这个属性不好使了,失去了它应有的作用

问题究竟出在哪里了呢?

当作为父layout的LinearLayout的属性为android:orientation="vertical" 的时候,android:layout_gravity="?"这里设为横向的时候才能生效。比如:left,right,center_horizontal等;
当作为父layout的LinearLayout的属性为android:orientation="horizental" 的时候,android:layout_gravity="?"这里设为纵向的时候才能生效。比如:top,bottom,center_vertical等;
有一个比较特殊的是center,不管是横向还是纵向的时候,它总有一个方向起作用, 因为LinearLayout他只可能有一个方向,

这nm的,确实让人蛋疼。其实也有点道理吧,就是LinearLayout横向的时候,如果有多个孩子,那就不知道把谁放最右了,

有两个解决方法吧,

(1)用RelativeLayout吧,这个算是费话吧 ,哈哈

(2)在LinearLayout中设置android:gravity这个从官方api的解释是怎么放置它的内容,LinearLayout的内容不就是他的孩子么,问题解决

现在根据它的提示,进行验证

1)在LinearLayout 中添加gravity属性

<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
     android:background="#ff0000"
android:orientation="horizontal"
android:paddingBottom="21dip"
android:gravity="bottom"
android:paddingLeft="@dimen/setup_fragment_padding_left"
android:paddingRight="@dimen/setup_fragment_padding_right" > <Button
android:id="@+id/manual_setup"
style="@style/accountSetupButtonVfive"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/button_margin_left"
android:background="@drawable/email_btn_set"
android:text="@string/account_setup_basics_manual_setup_action" /> <Button
android:id="@+id/next"
style="@style/accountSetupButtonVfive"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="@drawable/email_btn_next"
android:layout_marginRight="@dimen/button_margin_right"
android:text="@string/next_action" />
</LinearLayout>

结果:

所以,gravity是有效的

2)在单个button中添加layout_gravity属性

代码

<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#ff0000"
android:orientation="horizontal"
android:paddingBottom="21dip"
android:paddingLeft="@dimen/setup_fragment_padding_left"
android:paddingRight="@dimen/setup_fragment_padding_right" > <!-- Buttons below -->
<!--
In order to show these buttons above the IME keyboard, we need to special case the to
padding to a smaller height.
-->
<Button
android:id="@+id/manual_setup"
style="@style/accountSetupButtonVfive"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/button_margin_left"
android:background="@drawable/email_btn_set"
android:layout_gravity="bottom"
android:text="@string/account_setup_basics_manual_setup_action" /> <Button
android:id="@+id/next"
style="@style/accountSetupButtonVfive"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="@drawable/email_btn_next"
android:layout_marginRight="@dimen/button_margin_right"
android:text="@string/next_action" />
</LinearLayout>

结果如下

有效果的

3)如果添加的是layout_gravity="right"属性,已经验证没效果。

这就证明了上述结论

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

LinearLayout-layout_gravity 属性没有效果分析的更多相关文章

  1. android:gravity 和 android:layout_Gravity属性

    LinearLayout有两个非常相似的属性: android:gravity 与android:layout_gravity. 他们的区别在于: android:gravity 属性是对该view中 ...

  2. 【Android布局】在程序中设置android:gravity 和 android:layout_Gravity属性

    在进行UI布局的时候,可能常常会用到 android:gravity  和 android:layout_Gravity 这两个属性. 关于这两个属性的差别,网上已经有许多人进行了说明,这边再简单说一 ...

  3. ZT Android布局】在程序中设置android:gravity 和 android:layout_Gravity属性

    Android布局]在程序中设置android:gravity 和 android:layout_Gravity属性 分类: [Android基础] 2011-04-19 16:06 54739人阅读 ...

  4. 【Android布局】在程序中设置android:gravity 和 android:layout_Gravity属性——位置设置偏向

    LinearLayout有两个非常相似的属性: android:gravity与android:layout_gravity. 他们的区别在于: android:gravity 属性是对该view中内 ...

  5. 【转】在程序中设置android:gravity 和 android:layout_Gravity属性

    在进行UI布局的时候,可能经常会用到 android:gravity  和 android:layout_Gravity 这两个属性. 关于这两个属性的区别,网上已经有很多人进行了说明,这边再简单说一 ...

  6. Android布局(一)layout_gravity 属性和 gravity属性的区别

    安卓中的 layout_gravity 属性和 gravity属性 有啥区别? LinearLayout有两个非常相似的属性: android:gravity与android:layout_gravi ...

  7. iOS开发之虾米音乐频道选择切换效果分析与实现

    今天博客的内容比较简单,就是看一下虾米音乐首页中频道选择的一个动画效果的实现.之前用mask写过另外一种Tab切换的一种效果,网易云音乐里边的一种Tab切换效果,详情请移步于"视错觉:从一个 ...

  8. jQuery RemoveAttr(checked)之后再Attr(checked)属性无效果的原因分析

    jQuery中attr()和prop()在修改checked属性时的区别 投稿:whsnow 字体:[增加 减小] 类型:转载   使用语句$.attr('checked',true),将复选框的属性 ...

  9. avalon全选效果分析讲解

    全选功能就是 1.点击全选控制循环元素是否选中.(点击全选,下面的所有元素选中,再次点击 所有元素取消选中.) 2.点击循环元素控制全选.(如果当前元素是未选中状态则全选不选中,如果当前元素是选中状态 ...

随机推荐

  1. VMware下最小化安装centos 7 后上网设置

    1.文件/etc/sysconfig/network-scripts/ifcfg-eno16777736 将ONBOOT=no 改为 ONBOOT=yes 保存 2.重启网卡:service netw ...

  2. Bit、Byte、kb、KB、MB、KiB、MiB各表示什么意思?

    1.Byte.bit:bit表示:位,是计算机中最小的数据单位.每一位的状态只能是0或1Byte表示:字节,8个二进制位构成1个字节,即1 Byte = 8 bit .1个英文字母或者数字占用1个字节 ...

  3. LRJ入门经典-0903切蛋糕305

    原题 LRJ入门经典-0903切蛋糕305 难度级别:B: 运行时间限制:1000ms: 运行空间限制:256000KB: 代码长度限制:2000000B 试题描述 如图所示有一个矩形蛋糕,上面划分成 ...

  4. jquery2.0.3 全部源码

    /*! * Includes Sizzle.js 选择器,独立的库 * http://sizzlejs.com/ */ (function( window, undefined ) { //" ...

  5. 59.C++与正则表达式

    regex_match 整个字符串是否匹配 (通过cmatch存储匹配的结果),match[0]代表整个匹配序列,match[1]代表第1个匹配后的子序列,match[2]代表第2个匹配后的子序列 代 ...

  6. POJ 3175 枚举

    思路: 枚举小数点前 的数是啥 判一判 复杂度是根号的-.. 注意精度!!!! //By SiriusRen #include <cmath> #include <cstdio> ...

  7. Self-Taught Learning

    the promise of self-taught learning and unsupervised feature learning is that if we can get our algo ...

  8. C++ 补课(一)

    1,在C语言中,全局变量必须声明在所有的函数之前,局部变量必须声明在所有可执行语句之前: C++ 允许在代码块的任何位置对局部变量进行声明 2,常量定义方面,C语言 #define 可能因计算的优先级 ...

  9. Linux下关机命令的区别 (halt,poweroff,reboot,shutdown,init)

    1.shutdown shutdown命令安全地将系统关机.    而在系统关机前使用shutdown命令﹐系统管理员会通知所有登录的用户系统将要关闭.并且login指令会被冻结﹐即新的用户不能再登录 ...

  10. C++集合运算函数总结 & 需要有序集合的操作

    前提:两个集合已经有序.merge() //归并两个序列,元素总个数不变,只是将两个有序序列归并为一个有序序列.set_union() //实现求集合A,B的并.set_difference()//实 ...