安卓基础之按比例布局layout_weight和weightSum
2015-07-22 23:19
447 查看
先看一个效果,一个Button占据整个屏幕的一半宽度。
再看开发文档中对layout_weight属性的描述:
“定义weight总和的最大值。如果未指定该值,以所有子视图的layout_weight属性的累加值作为总和的最大值。一个典型的案例是:通过指定子视图的layout_weight属性为0.5,并设置LinearLayout的weightSum属性为1.0,实现子视图占据可用宽度的50。”
XML文件仅仅包含一个Button,它的宽度占据整个屏幕的一半,代码如下:
[html] view
plaincopy
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="1" >
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="@string/activity_main_click_me" />
</LinearLayout>
在上面的xml中,指定Button的android:layout_width属性为0dp,因此需要根据android:weightSum属性决定Button的width。
假设有一个宽度是200dp,android:weightSum属性是1.0的LinearLayout。
在这个LinearLayout中的Button宽度的计算公式如下:
[plain] view
plaincopy
Button's width + Button's weight * 200 / sum(weight)
指定Button的width为0dp,weight为0.5,sum(weight)等于1,那么结果如下。
0 + 0.5 * 200 / 1 = 100
当需要根据比例分配布局可用空间的时候,使用LinearLayout的weight属性是很有必要的,这避免了使用硬编码的方式带来的副作用。
如果目标平台是Honeycomb并且使用Fragment,那么大多数案例中都是使用weight在布局文件中为Fragment分配空间。
深入理解如何使用weight会为开发者增添一项重要技能。
参考资料
http://developer.android.com/reference/android/widget/LinearLayout.html
http://mobile.51cto.com/abased-375428.htm
转载自:http://blog.csdn.net/manoel/article/details/38968577
再看开发文档中对layout_weight属性的描述:
“定义weight总和的最大值。如果未指定该值,以所有子视图的layout_weight属性的累加值作为总和的最大值。一个典型的案例是:通过指定子视图的layout_weight属性为0.5,并设置LinearLayout的weightSum属性为1.0,实现子视图占据可用宽度的50。”
XML文件仅仅包含一个Button,它的宽度占据整个屏幕的一半,代码如下:
[html] view
plaincopy
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="1" >
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="@string/activity_main_click_me" />
</LinearLayout>
在上面的xml中,指定Button的android:layout_width属性为0dp,因此需要根据android:weightSum属性决定Button的width。
假设有一个宽度是200dp,android:weightSum属性是1.0的LinearLayout。
在这个LinearLayout中的Button宽度的计算公式如下:
[plain] view
plaincopy
Button's width + Button's weight * 200 / sum(weight)
指定Button的width为0dp,weight为0.5,sum(weight)等于1,那么结果如下。
0 + 0.5 * 200 / 1 = 100
当需要根据比例分配布局可用空间的时候,使用LinearLayout的weight属性是很有必要的,这避免了使用硬编码的方式带来的副作用。
如果目标平台是Honeycomb并且使用Fragment,那么大多数案例中都是使用weight在布局文件中为Fragment分配空间。
深入理解如何使用weight会为开发者增添一项重要技能。
参考资料
http://developer.android.com/reference/android/widget/LinearLayout.html
http://mobile.51cto.com/abased-375428.htm
转载自:http://blog.csdn.net/manoel/article/details/38968577
相关文章推荐
- C++构造函数
- mysql1175错误
- java的值传递与引用传递试验
- C++面向对象编程
- OpenGL Shader in OpenCASCADE
- 变量一般不要定义在.h文件中
- java基础之产生随机数
- Tomcat使用技巧最新总结
- JavaScript之判断用户登录信息
- Backbone之旅——Model篇
- 探索MySQL高可用架构之MHA(9)
- 黑马程序员——第八天(String类)
- 分布式 ZooKeeper 缓存用法实例教程
- java基础之自动装箱和拆箱
- 从C语言开始搞iOS开发
- NSSearchPathForDirectoriesInDomains方法使用
- 数据库 将多条SQL语句查询的内容整合成为一个元组
- 43. Multiply Strings
- POJ 2255 Tree Recovery 树的遍历,分治 难度:0
- 德国精品软件 Throttle 网络加速软件