转自: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. Codeforces Round #298 (Div. 2) D. Handshakes [贪心]

    传送门 D. Handshakes time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  2. msp430项目编程02

    msp430中项目---液晶1602显示 1.液晶1602工作原理 2.电路原理说明 3.代码(静态显示) 4.代码(动态显示) 5.项目总结 msp430项目编程 msp430入门学习

  3. ACM-ICPC 2018 沈阳赛区网络预赛 G 容斥原理

    https://nanti.jisuanke.com/t/31448 解析 易得an=n*n+n O(1)得到前n项和  再删除与m不互素的数  我们用欧拉函数求出m的质因数  枚举其集合的子集 进行 ...

  4. Codeforces 920E(补图BFS)

    题意: n(n<=200000)个点的完全图删去了m(m<=200000)条边,求剩下图的连通分量. 分析: 将未访问过的点用一个链表串起来 仍旧进行BFS,每次BFS扩展一个点u的时候, ...

  5. Netflix是什么,与Spring Cloud有什么关系

    说明:以下总结的观点不一定准确,但是是最好理解的. 1.首先,Netflix是一家做视频的网站,可以这么说该网站上的美剧应该是最火的. 2.Netflix是一家没有CTO的公司,正是这样的组织架构能使 ...

  6. eclipse提速02 - eclipse.ini优化

    给eclipse执行jvm.它可以让你使用自己的jdk,而不是系统环境变量所指定的jdk -vm /path/to/your/java 使用最新的jdk来运行eclipse.使用最新的jdk要好很多. ...

  7. 常见machine learning模型实现

    一.感知机模型 二.线性回归(Linear Regression) from numpy import * def loadData(filename): x = [] y = [] f = open ...

  8. Github配置SSH

    以前也配置过ssh,但是没有注意用法,在配置一次熟悉流程 检查本机是否有ssh key设置 $ cd ~/.ssh 或cd .ssh 如果没有则提示: No such file or director ...

  9. Echarts 的样例

    jsp页面: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8 ...

  10. Unity UGUI——概述、长处

    Unity4.6推出的新UI系统 长处:灵活.高速.可视化.效率高效果好.易于使用和扩展