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.点击循环元素控制全选.(如果当前元素是未选中状态则全选不选中,如果当前元素是选中状态 ...
随机推荐
- Controller接口控制器2
5.ServletForwardingController 将接收到的请求转发到一个命名的servlet,具体示例如下: package cn.javass.chapter4.web.servlet; ...
- POJ 1895 分层图网络流+输出路径
题意: 题目描述:在公元3141年人类的足迹已经遍布银河系.为了穿越那巨大的距离,人类发明了一种名为超时空轨道的技术.超时空轨道是双向的,连接两个星系,穿越轨道需要一天的时间.然而这个轨道只能同时给一 ...
- OPENCV(7) —— HighGUI
包括函数createTrackbar.getTrackbarPos.setTrackbarPos.imshow.namedWindow.destroyWindow.destroyAllWindows. ...
- Linux下通过.desktop 文件创建桌面程序图标及文件编写方式(Desktop Entry文件概述)
Linux下通过.desktop 文件创建桌面程序图标及文件编写方式 1.Desktop Entry文件概述:在 Windows 平台上,用户可以通过点击位于桌面或菜单上的快捷方式轻松打开目标应用程序 ...
- MongoDB常用操作整理
Mongodb:是一种NoSQL数据库,NoSQL:Not Only SQLSQL: 数据表->JDBC读取->POJO(VO.PO)->控制层转化为JSON数据->客户端 这 ...
- mysql分页小结
mysql.select('*').from('books') .join('cSessionInfo', 'books.openid', 'cSessionInfo.open_id') .limit ...
- 今日SGU 5.15
最近事情好多,数据库作业,没天要学2个小时java,所以更新的sgu就比较少了 SGU 131 题意:给你两种小块一种,1*1,一种2*2-1*1,问你填满一个m*n的矩形有多少钟方法,n和m小于等于 ...
- 使用 HTML5 canvas制作拾色器
自制的拾色器漂亮吧,哈哈 废话不多说直接上代码,希望可以帮到需要的朋友 <html><head> <style> .canvas_color{p ...
- Swift学习笔记(15)--下标脚本(Subscripts)
下标脚本可以定义在类(Class).结构体(structure)和枚举(enumeration)这些目标中,使用中类似数组或者字典的用法 1.定义 定义下标脚本使用subscript关键字,语法: s ...
- C# 对Excel操作时,单元格值的读取
一.Range中Value与Value2的区别 当range("A1:B10")设置为 Currency (货币)和 Date (日期.日期时间)数据类型时,range2将返回对应 ...