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

 <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. 用Struts2搭建一个登录例子【本人亲测好用】

    今天尝试struts2的搭建,遇到不少的问题,终于一一解决,逛了很多地方,最终完成搭建 1.首先要下载struts2的一些组件,我下载的是版本2.3.4.1,Eclipse是4.6版本的.由于版本的不 ...

  2. Web 端 js 导出csv文件

    http://www.qdfuns.com/notes/35821/2ab249182734d1f5c66da6b5cf395db9.html

  3. DataTable中Compute计算函数

    DataTable dt = new DataTable(); //嵌套的三元运算 牛叉到五体投地 object obj = dt.Compute("iif(1000=5,1000,iif( ...

  4. JSP页面的静态包含和动态包含的区别与联系

    JSP中有两种包含: 静态包含:<%@include file="被包含页面"%> 动态包含:<jsp:include page="被包含页面" ...

  5. du---是对文件和目录磁盘使用的空间查看

    du命令也是查看使用空间的,但是与df命令不同的是Linux du命令是对文件和目录磁盘使用的空间的查看,还是和df命令有一些区别的. 语法 du [选项][文件] 选项 -a或-all 显示目录中个 ...

  6. CodeForcesGym 100502D Dice Game

    Dice Game Time Limit: 1000ms Memory Limit: 524288KB This problem will be judged on CodeForcesGym. Or ...

  7. hiho week 38 P1 : 二分·二分答案

    P1 : 二分·二分答案 Time Limit:10000ms Case Time Limit:1000ms Memory Limit:256MB 描述 在上一回和上上回里我们知道Nettle在玩&l ...

  8. CentOs虚拟机能够互相ping通,但无法訪问虚拟机服务

    CentOs虚拟机能够互相ping通,但无法訪问虚拟机服务 虚拟机ip:192.168.0.57 主机 ip:192.168.0.80 在虚拟机上搭建了php环境.虚拟机CentOs,主机win7 虚 ...

  9. mongodb适用和不适用的应用场景

    近期考虑把订单历史数据从Oracle数据库迁移到Nosql数据库做历史数据查询和分析,一天千万级数据.打算使用mongodb数据库.使用nodejs做查询和统计API,对并发请求量要求低,不知道有没有 ...

  10. js --- 事件流

    1.事件流 事件发生时会在元素节点与根节点之间按照特定的顺序传播,路径所经过的所有节点都会收到该事件,这个传播过程即DOM事件流. 2.两种事件流模型 1.冒泡型事件流:事件的传播是从最特定的事件目标 ...