Android开发笔记—— layout_weight
来源:互联网 发布:矩阵相似和合同的区别 编辑:程序博客网 时间:2024/06/08 05:31
在我们日常开发中,LinearLayout是我们经常用到的布局组件,其中有一个属性layout_weight属性在我们的布局屏幕适配中起了很重要作用,但是当设置这一属性以后,再设置组件宽度会出现不同的状况:
当组件宽度设置为match_parent时:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.5" android:text="ok1sssssss" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1.0" android:text="ok2" /></LinearLayout>
效果是这样
显示结果正好是2:1 跟我们设置的权值相反
当我们把宽度设置为warp_content时:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.5" android:text="ok1sssssss" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1.0" android:text="ok2" /></LinearLayout>
效果如下:
在我刻意将button1的文字加长以后,加上权值以后,最终效果近似于1:1的效果。
那么问题来了,这个权值到底是不是比例,我们应该怎么样来使用它?
在这里weight的实际含义其实是:
一旦该组件指定了weight属性,那么该组件的宽度就等于该组件宽度(layout_width)加上剩余空间所占比
设屏幕宽度为L,在两个view的宽度都为match_parent的情况下,原有宽度为L,两个的View的宽度都为L,那么剩余宽度为L-(L+L) = -L, 左边的View占比三分之一,所以总宽度是L+(-L)*1/3 = (2/3)L.所以出现为什么权重为0.5的反而长度为权重为1的两倍,而在warp_content中,权重为0.5的字符串长度长一些,最终效果就类似一比一。
实际上,谷歌推荐的方法就是在使用权重属性时,将该组件的宽度设为0,然后得到的效果就是和权重比相匹配的效果:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.5" android:text="ok1sssss" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1.0" android:text="ok2" /></LinearLayout>
结论:当我们使用layout_weight属性的时候,需要将组件的宽度或高度(看父类标签是横向还是纵向)设置为0dp,此时组件就是按照权重比来确定大小的,这在屏幕适配中非常常见
0 0
- Android开发笔记—— layout_weight
- android开发笔记之权重(layout_weight)
- Android笔记之layout_weight
- android开发笔记之android:layout_weight的使用
- Android 笔记 linearlayout 属性 layout_weight
- 重修Android之新知识——layout_weight
- Android学习笔记(四)layout_weight
- Android笔记分享---Layout_weight的深刻理解
- Android学习笔记(二):layout_weight的解读
- Android-Layout_weight属性解析(慕课网笔记)
- Android学习笔记----layout_weight属性解析
- Android开发之layout_weight属性的应用
- Android开发中关于Layout_weight的理解
- android UI布局设计 —— layout_weight浅见
- Android基础之——Linerlayout中layout_weight的使用
- Android 布局学习之——LinearLayout的layout_weight属性
- Android Studio——layout_weight体验(实现按比例显示)
- android:layout_weight
- period task
- 【Android之隐藏状态栏、设置全屏、取消全屏】
- axis2集成到已有项目操作实例(含源代码)
- UIViewContentMode各类型效果
- 最简单的基于Flash的流媒体示例:网页播放器(HTTP,RTMP,HLS)
- Android开发笔记—— layout_weight
- Android实战简易教程-第十九枪(SwipeRefreshLayout下拉刷新使用实例)
- 自己做QQ–后台搭建
- PP生产订单状态显示(rel等)如何查询表,字段
- 学长们的求职血泪史(C/C++/JAVA)
- Android MaterialDesign中颜色
- 【我们都爱Paul Hegarty】斯坦福IOS8公开课个人笔记33 LifeCycle生命周期
- Android手机分辨率基础知识(DPI,DIP计算)
- python glob模块