LinearLayout中通过layout weight属性来表示这个孩子视图view在整个手机屏幕占的大小比例。

一般不在孩子视图里面写这个属性的时候,则默认为0

android:layout_weight="0"

For example, if there are three text fields and two of them declare a weight of 1, while the other is given no weight, the third text field without weight will not grow and will only occupy the area required by its content. The other two will expand equally to fill the space remaining after all three fields are measured. If the third field is then given a weight of 2 (instead of 0), then it is now declared more important than both the others, so it gets half the total remaining space, while the first two share the rest equally.

比如,如果一个LinearLayout有3个text的孩子视图,其中2个声明layout_weight=1;另外一个没有声明,即默认为0,占用的空间大小应该和它的内容一样。那么这2个声明了为1的,就平分剩下的空间。

如果第3个声明不是默认为0而是1的话,那么第3个声明占用了全部空间的一半,另外2个声明了1的平分另外的一半.

看下面一个例子:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:paddingLeft="16dp"android:paddingRight="16dp"android:orientation="vertical" ><EditTextandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:hint="@string/to" /><EditTextandroid:layout_width="fill_parent"android:layout_height="0dp"android:layout_weight="1"android:hint="@string/subject" /><EditTextandroid:layout_width="fill_parent"android:layout_height="0dp"android:layout_weight="1"android:gravity="top"android:hint="@string/message" /><Buttonandroid:layout_width="100dp"android:layout_height="wrap_content"android:layout_gravity="top"android:text="@string/send" />
</LinearLayout>

上面运行的结果如下:

明显to和send所在的区域,因为默认layout_weight不显示;所以大小默认为0,占用的空间为内容大小。

其他的2个edit平均占用剩下的空间。

关于

 android:layout_height="0dp"android:layout_weight="1"

为什么药这样设置,可以看文档上面的这段话:

Equally weighted children

To create a linear layout in which each child uses the same amount of space on the screen, set theandroid:layout_height of each view to "0dp" (for a vertical layout) or theandroid:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1".

转载于:https://my.oschina.net/liangzhenghui/blog/192361

关于Layout Weight一些使用技巧相关推荐

  1. android 高度上分权重,安卓自适应布局(关于权重weight的使用技巧!)

    weight和weightSum Layout Weight LinearLayout also supports assigning a weight to individual children ...

  2. android+动态设置权重,Android自适应布局(关于权重weight的使用技巧!)

    weight和weightSum Layout Weight LinearLayout also supports assigning a weight to individual children ...

  3. LayOut 制作思路与技巧分享 | “小红人”风格建筑分析图

    作者:活力网Andrew 大家好,今天为大家带来的是 分享LayOut制作建筑分析图的思路与技巧. 这是一套西班牙建筑师卢西亚·纳瓦罗·科丘拉 在秘鲁 利马的城市规划项目分析图. 笔者最近正巧在研究 ...

  4. 灵活运用CSS开发技巧(转载)

    何为技巧,意指表现在文学.工艺.体育等方面的巧妙技能.代码作为一门现代高级工艺,推动着人类科学技术的发展,同时犹如文字一样承托着人类文化的进步. 每写好一篇文章,都会使用大量的写作技巧.烘托.渲染.悬 ...

  5. css鼠标拖拉卡顿_66个值得收藏的CSS开发技巧

    来源:https://mp.weixin.qq.com/s/hEWqQYfrbTrEJ4CIa16DCQ 作者:前端宇宙 何为技巧,意指表现在文学.工艺.体育等方面的巧妙技能.代码作为一门现代高级工艺 ...

  6. Layout室内设计施工图——PDF矢量图纸输出和显示模式区别

    作者:活力Andrew 施工图绘画好之后,最终还是要以纸质图纸呈现 不可能直接把图丢给打印店让他们直接打印 所以需要我们将绘好的图纸输出为PDF格式 然后整合成一个文件 那如何输出我们图纸? 打开我们 ...

  7. 计算机视觉(CV)-生成模型:WGAN【Lipschitz:Weight Clipping】--> WGAN-GP【Lipschitz:Gradient Penalty】

    深度学习-生成模型:WGAN[Lipschitz:Weight Clipping]--> WGAN-GP[Lipschitz:Gradient Penalty] 一.WGAN相比较GAN的改进点 ...

  8. 【转】RelativeLayout和LinearLayout及FrameLayout性能分析

    原文:http://blog.csdn.net/hejjunlin/article/details/51159419 工作一段时间后,经常会被领导说,你这个进入速度太慢了,竞品的进入速度很快,你搞下优 ...

  9. Android性能优化2

    Android中性能优化的问题.本篇文章是对前面部分的一个总结(读者可以翻看之前的文章),同时笔者结合了实际项目中的经验,希望能对大家抛砖引玉. Android性能优化,也是面试中几乎必问的知识点.本 ...

最新文章

  1. 测量工具(keras)
  2. SQL 流程控制语句 之四 WAITFOR语句介绍
  3. linux shell顺序执行,shell 执行顺序
  4. linux内核态串口读写程序,linux 0.11 内核学习 -- rs_io.s,串口汇编代码
  5. 还在为多集群管理烦恼吗?RedHat 和蚂蚁、阿里云给开源社区带来了OCM
  6. leetcode 232. 用栈实现队列 思考分析
  7. arcgis判断两个字段是否相等
  8. Spring加载资源文件的方式
  9. 苏宁易购出售苏宁消费金融39%股权
  10. 韩语在线翻译图片识别_一键截图识别屏幕文字,支持实时翻译还能朗读
  11. OpenCV精进之路(九):图像轮廓和图像分割修复——图像修复技术
  12. hive -e执行命令报错
  13. 内部人示范(野生项目笔记02)
  14. Java文件上传之断点续传解决方案
  15. 图片文字识别 mysql_有道智云OCR图片识别文字+返回数据处理技巧(实现语言-按键精灵脚本请求识别+java服务端处理数据)...
  16. lisp princ详解_晓东CAD家园-论坛-A/VLISP-正则表达式lisp实例讲解-通过代码,完整的展示了正则表达式在lisp中使用 - Powered by Discuz!...
  17. linux 命令 unicode,linux下中文转unicode
  18. 算法设计之动态规划法
  19. 数据结构课设——单位员工通讯录管理系统
  20. 2021-07-062021年危险化学品生产单位安全生产管理人员最新解析及危险化学品生产单位安全生产管理人员证考试

热门文章

  1. NYOJ 752 又见回文串
  2. 计算某天是星期几的方法
  3. Golang之gjson
  4. poi控制简单的word
  5. iOS--获取输入字符的第一个字母(汉字则获取拼音的第一个字母)
  6. 微软的最高市值是多少?
  7. 3月16日 winform
  8. POJ 2420 A Star not a Tree?【爬山法】
  9. [EF4] CompiledQuery预编译性能提升 + 数据载入之大彻大悟
  10. 向SharePoint页面添加后台代码