50个Android开发技巧(01 好好利用layout_weight属性)
来源:互联网 发布:算法复杂性理论 编辑:程序博客网 时间:2024/06/11 23:39
问题:如何将一个Button放置在布局中间并将其宽度设为其parent的50%?
分析:问题想要达到的效果应该是这样:
(原文地址:http://blog.csdn.net/vector_yi/article/details/24397733)
(原文地址:http://blog.csdn.net/vector_yi/article/details/24397733)
这看起来不难,但很多开发者并不知道达到这样效果的最佳方法。
解决:在此我们将weightSum属性与layout_weight属性一起利用。
<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" ><!--1.添加android:weightSum属性--> <Button android:layout_width ="0dp"<!--2.将Button的layout_width设为0dp--> android:layout_height ="wrap_content" android:layout_weight ="0.5"<!--3.确保其占用了50%的可用空间--> android:text ="@string/activity_main_click_me" /></LinearLayout>
可以注意到,在第2步将Button的layout_width设为了0dp,会不会与layout_weight有冲突?答案是不会:
一个控件的宽度是这样计算出来的:
Widget's width + Widget's weight*Parent's width/Parent's weightSum
10 1
- 50个Android开发技巧(01 好好利用layout_weight属性)
- 50个Android开发技巧(01 好好利用layout_weight属性)
- 50个Android开发技巧(01 好好利用layout_weight属性)
- 50个Android开发技巧(01 好好利用layout_weight属性)
- 50个Android开发技巧(1,使用android:layout_weight和android:weightSum属性的使用)
- 安卓开发技巧一:深入理解Android布局中Layout_weight的属性
- Android开发之layout_weight属性的应用
- android---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:layout_weight属性详解
- android:layout_weight属性讲解
- Android layout_weight属性
- android 浅谈消息处理机制
- Eclipse下编译C/C++程序出错:The program file specified in the launch configuration does not exist
- 路由器 集线器 交换机
- [数论+二分求等比数列]POJ 1845 Sumdiv
- Request.Form 和 Request.QueryString
- 50个Android开发技巧(01 好好利用layout_weight属性)
- 【机器学习算法-python实现】决策树-Decision tree(1) 信息熵划分数据集
- 简单工厂模式
- HBase-0.98.0和Phoenix-4.0.0分布式安装指南
- c4 Internet Address - Some useful Program
- C++第9周(春)项目5 - 一元一次方程类
- 连接数据库在JTable中显示
- maven(3)--依赖问题,依赖和继承
- RBF网络