LinearLayout-layout_gravity 属性没有效果分析
今天在一个布局文件中,遇到了一个问题,先看代码
<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 属性没有效果分析的更多相关文章
- android:gravity 和 android:layout_Gravity属性
LinearLayout有两个非常相似的属性: android:gravity 与android:layout_gravity. 他们的区别在于: android:gravity 属性是对该view中 ...
- 【Android布局】在程序中设置android:gravity 和 android:layout_Gravity属性
在进行UI布局的时候,可能常常会用到 android:gravity 和 android:layout_Gravity 这两个属性. 关于这两个属性的差别,网上已经有许多人进行了说明,这边再简单说一 ...
- ZT Android布局】在程序中设置android:gravity 和 android:layout_Gravity属性
Android布局]在程序中设置android:gravity 和 android:layout_Gravity属性 分类: [Android基础] 2011-04-19 16:06 54739人阅读 ...
- 【Android布局】在程序中设置android:gravity 和 android:layout_Gravity属性——位置设置偏向
LinearLayout有两个非常相似的属性: android:gravity与android:layout_gravity. 他们的区别在于: android:gravity 属性是对该view中内 ...
- 【转】在程序中设置android:gravity 和 android:layout_Gravity属性
在进行UI布局的时候,可能经常会用到 android:gravity 和 android:layout_Gravity 这两个属性. 关于这两个属性的区别,网上已经有很多人进行了说明,这边再简单说一 ...
- Android布局(一)layout_gravity 属性和 gravity属性的区别
安卓中的 layout_gravity 属性和 gravity属性 有啥区别? LinearLayout有两个非常相似的属性: android:gravity与android:layout_gravi ...
- iOS开发之虾米音乐频道选择切换效果分析与实现
今天博客的内容比较简单,就是看一下虾米音乐首页中频道选择的一个动画效果的实现.之前用mask写过另外一种Tab切换的一种效果,网易云音乐里边的一种Tab切换效果,详情请移步于"视错觉:从一个 ...
- jQuery RemoveAttr(checked)之后再Attr(checked)属性无效果的原因分析
jQuery中attr()和prop()在修改checked属性时的区别 投稿:whsnow 字体:[增加 减小] 类型:转载 使用语句$.attr('checked',true),将复选框的属性 ...
- avalon全选效果分析讲解
全选功能就是 1.点击全选控制循环元素是否选中.(点击全选,下面的所有元素选中,再次点击 所有元素取消选中.) 2.点击循环元素控制全选.(如果当前元素是未选中状态则全选不选中,如果当前元素是选中状态 ...
随机推荐
- POJ 1330 Nearest Common Ancestors 倍增算法的LCA
POJ 1330 Nearest Common Ancestors 题意:最近公共祖先的裸题 思路:LCA和ST我们已经很熟悉了,但是这里的f[i][j]却有相似却又不同的含义.f[i][j]表示i节 ...
- JAVA线程队列BlockingQueue
JAVA线程队列BlockingQueue 介绍 BlockingQueue阻塞队列,顾名思义,首先它是一个队列,通过一个共享的队列,可以使得数据由队列的一端输入,从另外一端输出. 常用的队列主要有以 ...
- wc---计算字数。
Linux wc命令用于计算字数. 利用wc指令我们可以计算文件的Byte数.字数.或是列数,若不指定文件名称.或是所给予的文件名为"-",则wc指令会从标准输入设备读取数据. 语 ...
- Linux学习总结(7)——阿里云centeros服务器上安装 jdk,tomcat,mysql
查看服务器的系统版本 # cat /etc/issue 查看服务器是64位还是32位 #uname -a 或者用:#getconf LONG_BIT 查看当前有没有安装jdk #rpm -q ...
- localtime死锁——多线程下fork子进程
近期測试我们自己改进的redis,发如今做rdb时,子进程会一直hang住.gdb attach上.堆栈例如以下: (gdb) bt #0 0x0000003f6d4f805e in __lll_lo ...
- Java解析注解
package com.itbuluoge.anno; import java.lang.reflect.Method; import java.util.ArrayList; import java ...
- vim-normal多行操作命令的使用
命令行命令-<:normal>这个命令可以重复上一个操作.他其实就跟.命令的效果查不到.不同的是,他可以把.的效果,作用于你用可视模式下的多行.例如,如果你想在下面的文字里在每一行加一个; ...
- RadioButton的check改变的时候
https://stackoverflow.com/questions/8095256/asp-net-radio-button-change You'll need to specify the a ...
- android 图片特效处理之怀旧效果
图片特效处理系列将介绍图片的像素点的特效处理,这些物资注重的是原理.也就是说只要你知道这些算法不管是C++,VB,C#,Java都可以做出相同的特效.下面将介绍图片怀旧效果的算法.算法如下: 上面公式 ...
- JSP_Learn
// 解决中文乱码的问题String name = new String((request.getParameter("name")).getBytes("ISO-885 ...