转自: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的异曲同工之妙(转载)的更多相关文章

  1. 伏羲八卦、文王六十四卦、老子阴阳太极、西方哲学辩证与"解耦和复用”思想的异曲同工之妙

    伏羲八卦.文王六十四卦.老子阴阳太极.西方哲学辩证与"解耦和复用”思想的异曲同工之妙     问题:任何程序语言在遇到复杂逻辑时,代码维护难度就会加大,如何处理该问题? 答案:重构,模块化. ...

  2. POJ-1325 Machine Schedule,和3041有着异曲同工之妙,好题!

    Machine Schedule Time Limit: 1000MS   Memory Limit: 10000K       Description As we all know, machine ...

  3. Oracle降低高水位先(转载)

    Oracle  降低高水位线的方法 高水位(HIGH WARTER MARK,HWM)好比水库中储水的水位,用于描述数据库中段的扩展方式.高水位对全表扫描方式有着至关重要的影响.当使用DELETE删除 ...

  4. (转载)经典计算机视觉论文笔记——DeepFace\DeepID\DeepID2\DeepID3\FaceNet\VGGFace汇总

    1. DeepFace:Closing the Gap to Human-Level Performance in Face Verification 最早将深度学习用于人脸验证的开创性工作.Face ...

  5. 多线程事儿(task)之 一(转载)

    此文转载作为记录,转载地址https://www.cnblogs.com/xiaoXuZhi/p/XYH_tsak_one.html 多线程,一个多么熟悉的词汇,作为一名程序员,我相信无论是从事什么开 ...

  6. C# Monitor.Wait() 源码追踪 (转载)

    source: 释放对象上的锁并阻止当前线程,直到它重新获取该锁. 如果已用指定的超时时间间隔,则线程进入就绪队列. 可以在等待之前退出同步上下文的同步域,随后重新获取该域. [SecuritySaf ...

  7. 教你如何利用分布式的思想处理集群的参数配置信息——spring的configurer妙用

    引言 最近LZ的技术博文数量直线下降,实在是非常抱歉,之前LZ曾信誓旦旦的说一定要把<深入理解计算机系统>写完,现在看来,LZ似乎是在打自己脸了.尽管LZ内心一直没放弃,但从现状来看,需要 ...

  8. 【Todo】【转载】深度学习&神经网络 科普及八卦 学习笔记 & GPU & SIMD

    上一篇文章提到了数据挖掘.机器学习.深度学习的区别:http://www.cnblogs.com/charlesblc/p/6159355.html 深度学习具体的内容可以看这里: 参考了这篇文章:h ...

  9. jQuery Mobile与QUI框架的异曲同工之处

    最近一直在研究jQuery Mobile框架,这是jQuery的官方移动版UI框架,专门用来开发手机与平板电脑方面的应用.结果越来越觉得它和我的QUI框架的开发思路非常相似,很多地方都有异曲同工之妙. ...

随机推荐

  1. 【BZOJ1225】求正整数(数论)

    题意:对于任意输入的正整数n,请编程求出具有n个不同因子的最小正整数m. n<=50000 思路:记得以前好像看的是maigo的题解 n即为将m分解为质数幂次的乘积后的次数+1之积 经检验只需要 ...

  2. JS基本数据类型&流程控制

    JS基本数据类型 number     -----  数值 boolean    -----  布尔值 string     -----  字符串 [x,y] -------数组 undefined  ...

  3. 一个APP的由来

    之前在站酷.UI中国.优设等网站看过不少的APP教程.规范等一些东西.自认为有些规范讲的内容过于繁琐,对于像我这样的大多数设计师来说看着看着就懵逼了....          

  4. BUPT2017 springtraining(16) #1 题解

    https://vjudge.net/contest/162590 A: 不难发现,当L=R时输出L,当L<R时输出2. B: 贪心得配对.1和n配 2和n-1配,对与对直接只要花1个代价就可以 ...

  5. Meteor计时器

    Meteor有提供它自己的setTimeout和setInterval方法.这些方法被用于确保所有全局变量都具有正确的值.它们就像普通 JavaScript 中的setTimeout 和 setInt ...

  6. CEF3研究(一)

    一.基本概览 C++ WrapperC++Wrapper(包装类)就是将C结构包装C++类. 这是C/C++API转换层通过translator tool自动产生的. 进程     CEF3用多进程运 ...

  7. 【Todo】STAR面试法

    今天在面试培训的时候,接触到了STAR面试法. 觉得挺好的,用来准备非技术面试,还蛮全面的.所以可以多了解一下. 可以参考:http://www.hrloo.com/rz/73652.html

  8. 聚类算法K-Means, K-Medoids, GMM, Spectral clustering,Ncut

    聚类算法是ML中一个重要分支,一般采用unsupervised learning进行学习,本文根据常见聚类算法分类讲解K-Means, K-Medoids, GMM, Spectral cluster ...

  9. C#:excel导入导出

    资源:excelService 服务 http://download.csdn.net/detail/istend/8060501 排列问题 导出时,数字和字符的排列格式默认不一样,数字靠右,字符靠左 ...

  10. 简述HashMap和Hashtable的差别

    1.HashMap继承AbstractMap类. Hashtable继承了Dictionary类. 2.HashMap同意有null的键和值.       Hashtable不同意有null的键和值. ...