Android layout_weight 和 weightSum
2016-02-25 10:51
375 查看
提问:如果一个布局里面只有一个子控件,怎么玩让它的宽度占一半?
这就涉及到上面两个属性了。对于layout_weight这个属性大家应该还是很熟悉的,但是对于weightSum我就很少用到了。实现上面的问题,具体代码如下:
<LinearLayout android:weightSum="2" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="0dp" android:id="@+id/btn_click" android:text="click" android:layout_weight="1" android:layout_height="wrap_content" /> </LinearLayout>
这个Button的宽度即为
Button's width + Button's weight * LinearLayout's width / sum'weight 也就是 Button's width + 所占比例* LinearLayout's width
所以,注意这里的Button里面的layout_width应该为0,而不是warp_match或者fill_match
说到layout_width的设置,这里面我们也说一下layout_width设置不同属性的情况。
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:text="1" android:layout_weight="1" android:background="@android:color/holo_purple" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:text="2" android:layout_weight="2" android:background="@android:color/holo_green_light" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:text="3" android:layout_weight="2" android:background="@android:color/holo_blue_light" /> </LinearLayout>
这也是我们正常看到的结果:1:2:2
代码做如下修改
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="1" android:layout_weight="1" android:background="@android:color/holo_purple" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="2" android:layout_weight="2" android:background="@android:color/holo_green_light" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="3" android:layout_weight="2" android:background="@android:color/holo_blue_light" /> </LinearLayout>
结果如下:3:1:1
这是为什么呢?
就因为修改了match_parent属性
你会发现 1的权重小,反而分的多了,这是为什么呢???网上很多人说是当layout_width=“match_parent”时,weight值越小权重越大,优先级越高,其 实他们并没有真正理解这个问题,真正的原因是Layout_width=”match_parent”的原因造成的。依照上面理解我们来分析:
系统先给3个textview分配他们所要的宽度match_parent,也就是说每一都是填满他的父控件,这里就死屏幕的宽度
那么这时候的剩余空间=1个parent_width-3个parent_width=-2个parent_width (parent_width指的是屏幕宽度 )
TextView 所占空间的计算
TextView宽度+权重比例*剩余空间大小
那么第一个TextView的实际所占宽度应该=fill_parent的宽度,即parent_width + 他所占剩余空间的权重比列1/5 * 剩余空间大小(-2 parent_width)=3/5parent_width
同理第二个TextView的实际所占宽度=parent_width + 2/5*(-2parent_width)=1/5parent_width;
第三个TextView的实际所占宽度=parent_width + 2/5*(-2parent_width)=1/5parent_width;所以就是3:1:1的比列显示了。
但是,个人觉得在实际的应用中没必要采用第二种去设置。用第一种就阔以简单的实现灵活的布局配置。
相关文章推荐
- android EventBus详解(一)
- android EventBus详解(一)
- android EventBus详解(一)
- Android AIDL, Parcelable和远程服务
- AndroidManifest.xml中android:configChanges的简介
- Android中的dispatchTouchEvent()、onInterceptTouchEvent()和onTouchEvent()
- Android控件
- Android的进程与线程
- android开发中圆角图片的绘制
- Android5 Zygote 与 SystemServer 启动流程分析
- Android知识结构图
- android测试工具MonkeyRunner--google官网翻译
- 获取Android正在运行的任务和服务
- Android动画合集之属性动画-初见
- android 中怎么让ListView可以无限循环加载 Adapter中的数据
- Android应用开发中使用Fragment的入门学习教程
- Android中的Adapter 详解(三)
- Android 二维码 生成和识别(附Demo源码)只有链接地址
- Android屏幕适配全攻略
- 2.1.2 android 项目技术点