RelativeLayout和layout_weight的异曲同工之妙(转载)
转自:http://ericbaner.iteye.com/blog/1161751

Android应用UI开发,对以上布局,可以使用RelativeLayout, 即:
Xml代码
<RelativeLayout
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:gravity="center_vertical"> <Button android:id="@+id/btn1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="search"/> <EditText android:id="@+id/edit1"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_toLeftOf="@id/btn1"
android:hint="input the key word"/> </RelativeLayout>
也可以使用LinearLayout的 layout_weight属性,即
Java代码
<LinearLayout
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:gravity="center_vertical">
<EditText
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:hint="input the key word" android:layout_weight=""/> <Button
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="search"/> </LinearLayout>
再多说几句layout_weight, Android SDK document中对layout_weight有如下解释:
Specifies how much of the extra space in the layout to be allocated to the View.
LinearLayout supports assigning a weight to individual children. This attribute assigns an "importance" value to a view, and allows it to expand to fill any remaining space in the parent view. Default weight is zero
calculation to assign any remaining space between child
space assign to child = (child individual weight) / (sum of weight of every child in Linear Layout)
Example (1): if there are three text boxes and two of them declare a weight of 1, while the third one is given no weight (0), then remaining space assign to
1st text box = 1/(1+1+0) 2nd text box = 1/(1+1+0) 3rd text box = 0/(1+1+0)
Example (2) : let's say we have a text label and two text edit elements in a horizontal row. The label has no layout_weight specified, so it takes up the minimum space required to render. If the layout_weight of each of the two text edit elements is set to 1, the remaining width in the parent layout will be split equally between them (because we claim they are equally important).
calculation : 1st label = 0/(0+1+1) 2nd text box = 1/(0+1+1) 3rd text box = 1/(0+1+1)
If the first one text box has a layout_weight of 1 and the second text box has a layout_weight of 2, then one third of the remaining space will be given to the first, and two thirds to the second (because we claim the second one is more important).
calculation : 1st label = 0/(0+1+2) 2nd text box = 1/(0+1+2) 3rd text box = 2/(0+1+2)
值得注意的是, layout_weight是对LinearLayout里剩余或多余的空间再分配给指定layout_weight的view元素。因此,在对每个 view元素的layout_width或layout_height写不同的值是可能有不同的结果的。 对本文最上面的图表示的一个文本编辑框和一个按钮,只有两个view元素, 文本框的layout_weight=1,而layout_width写fill_parent或wrap_content都是一样的。但若有两个文本框 和一个按钮同在一行,这两个文本框的layout_weight一个为1,一个为2,想要的效果是一个文本框的长度是另一个的2倍,要达到此效果,两文本 框的layout_width须设为0dip, 这样由layout_weight要控制每个文本框的宽度分配。
RelativeLayout和layout_weight的异曲同工之妙(转载)的更多相关文章
- 伏羲八卦、文王六十四卦、老子阴阳太极、西方哲学辩证与"解耦和复用”思想的异曲同工之妙
伏羲八卦.文王六十四卦.老子阴阳太极.西方哲学辩证与"解耦和复用”思想的异曲同工之妙 问题:任何程序语言在遇到复杂逻辑时,代码维护难度就会加大,如何处理该问题? 答案:重构,模块化. ...
- POJ-1325 Machine Schedule,和3041有着异曲同工之妙,好题!
Machine Schedule Time Limit: 1000MS Memory Limit: 10000K Description As we all know, machine ...
- Oracle降低高水位先(转载)
Oracle 降低高水位线的方法 高水位(HIGH WARTER MARK,HWM)好比水库中储水的水位,用于描述数据库中段的扩展方式.高水位对全表扫描方式有着至关重要的影响.当使用DELETE删除 ...
- (转载)经典计算机视觉论文笔记——DeepFace\DeepID\DeepID2\DeepID3\FaceNet\VGGFace汇总
1. DeepFace:Closing the Gap to Human-Level Performance in Face Verification 最早将深度学习用于人脸验证的开创性工作.Face ...
- 多线程事儿(task)之 一(转载)
此文转载作为记录,转载地址https://www.cnblogs.com/xiaoXuZhi/p/XYH_tsak_one.html 多线程,一个多么熟悉的词汇,作为一名程序员,我相信无论是从事什么开 ...
- C# Monitor.Wait() 源码追踪 (转载)
source: 释放对象上的锁并阻止当前线程,直到它重新获取该锁. 如果已用指定的超时时间间隔,则线程进入就绪队列. 可以在等待之前退出同步上下文的同步域,随后重新获取该域. [SecuritySaf ...
- 教你如何利用分布式的思想处理集群的参数配置信息——spring的configurer妙用
引言 最近LZ的技术博文数量直线下降,实在是非常抱歉,之前LZ曾信誓旦旦的说一定要把<深入理解计算机系统>写完,现在看来,LZ似乎是在打自己脸了.尽管LZ内心一直没放弃,但从现状来看,需要 ...
- 【Todo】【转载】深度学习&神经网络 科普及八卦 学习笔记 & GPU & SIMD
上一篇文章提到了数据挖掘.机器学习.深度学习的区别:http://www.cnblogs.com/charlesblc/p/6159355.html 深度学习具体的内容可以看这里: 参考了这篇文章:h ...
- jQuery Mobile与QUI框架的异曲同工之处
最近一直在研究jQuery Mobile框架,这是jQuery的官方移动版UI框架,专门用来开发手机与平板电脑方面的应用.结果越来越觉得它和我的QUI框架的开发思路非常相似,很多地方都有异曲同工之妙. ...
随机推荐
- 「CodePlus 2018 3 月赛」白金元首与克劳德斯
所有的云在此时没有重叠的面积 所有的云在此时没有重叠的面积 所有的云在此时没有重叠的面积 所有的云在此时没有重叠的面积 所有的云在此时没有重叠的面积 所有的云在此时没有重叠的面积 所有的云在此时没有重 ...
- java 基础 1 final关键字
1. final关键字 数据:声明数据为常量,对于基本类型final使数值不变,对于引用类型final使引用不变,但引用所指向的值是可以改变的,例如 final StringBuffer ...
- 使用Spring Data Redis操作Redis(集群版)
说明:请注意Spring Data Redis的版本以及Spring的版本!最新版本的Spring Data Redis已经去除Jedis的依赖包,需要自行引入,这个是个坑点.并且会与一些低版本的Sp ...
- 碧砚适合佳能328 4452 ICD520 4472 4450 硒鼓4700一体机墨盒4770
- Enhance Magento 404 page
Magento default installation already has a predefined custom 404 page (no-route). But is it enough t ...
- POJ 1679 The Unique MST 推断最小生成树是否唯一
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22715 Accepted: 8055 D ...
- LoadRunner 事务响应时间的组成
事务时间 一个事务的时间是指持续时间,事务会完全记录下从事务开始到事务结束之间的时间差,那么事务的时间能真实地反映业务操作的时间吗?不能,就好像人用手按秒表来记录短跑时间一样,得出的时间并不是完全准确 ...
- vs2010下配置CUDA出现kernel launch failed问题,内核无效
首先, 推荐一篇不错的配置文档~手把手教你 CUDA 5.5与VS2010编译环境的搭建.笔者就是在这篇文章的指导下成功地在VS2010上搭建了CUDA 6.5~ 其次. 文末给出的执行演示样例不好使 ...
- Android Studio keymap到Eclipse后,查找下一个同样变量快捷键Ctrl+K失效
注:升级到0.8的版本号以后.这个快捷键能够使了,只是另一个bug,假设你用了Ctrl+F先去查找了其它的东东,再使这个快捷键去定位另外一个变量可能偶尔会不灵,不灵的话还是能够用我以下的方式来让Ctr ...
- 《C++ Primer Plus》学习笔记3
<C++ Primer Plus>学习笔记3 第8章 函数探幽 ============================================================== ...