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

 <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. Redis之创建

    redis配置文件信息 public sealed class RedisConfigInfo { /// <summary> /// 可写的Redis链接地址 /// format:ip ...

  2. POJ 1167 The Buses 暴搜+剪枝

    思路: 先把能选的路线都预处理出来 按照能停的车的多少排个序 (剪枝1) 搜搜搜 如果当前剩的车÷当前能停车的多少+deep>=ans剪掉 (剪枝2) //By SiriusRen #inclu ...

  3. codeforces 710D Two Arithmetic Progressions(线性同余方程)

    题目链接: http://codeforces.com/problemset/problem/710/D 分析:给你两个方程 a1k + b1 and a2l + b2,求在一个闭区间[L,R]中有多 ...

  4. Important Abstractions and Data Structures

    For Developers‎ > ‎Coding Style‎ > ‎ Important Abstractions and Data Structures 目录 1 TaskRunne ...

  5. C/C++(C++拷贝构造器,赋值运算符重载)

    拷贝构造器 由己存在的对象,创建新对象.也就是说新对象,不由构造器来构造,而是由拷贝构造器来完成.拷贝构造器的格式是固定的. class 类名 { 类名(const 类名 & another) ...

  6. cz.msebera.android.httpclient.conn.ConnectTimeoutException: Connect to /192.168.23.1:8080 timed out(Android访问后台一直说链接超时)

    明明之前还是可以运行的练习,过段时间却运行不了,一直说访问后台超时, 对于这个问题我整整弄了两天加一个晚上,心酸...,上网找了很多但是都解决不了,我就差没有砸电脑了. 首先 : 第一步:Androi ...

  7. C# Socket服务端及多客户端连接通信实现

    服务端代码(控制台示例): static List<Socket> Sockets = new List<Socket>(); static void Main(string[ ...

  8. Volley简单学习使用五—— 源代码分析三

    一.Volley工作流程图: 二.Network     在NetworkDispatcher中须要处理的网络请求.由以下进行处理: NetworkResponse networkResponse = ...

  9. c++中六种构造函数的实现以及9中情况下,构造函数的调用过程

    六种构造函数的实现代码例如以下: #include<iostream> using namespace std; //c++中六种默认的构造函数 class Test { public: ...

  10. vue 引入第三方字体包

    1.创建 style_css 文件夹,在当前文件夹下 新建 index.css  和  引入字体包. 2. 在build 目录下,设置 webpack.base.cong.js ,  在module ...