Android中layout_weight的属性理解
https://www.zybuluo.com/zzudhj/note/102067
在Android开发过程中,在编写布局文件经常会用layout_weight属性:从字面意思上看权重、比值、按比例。。。
通过例子来看看layout_weight的用法
example1:
该布局有三部分组成,title、edit、bottom,其中,为了满足edit可以填充父窗口,可以为EditText添加layou_weight属性。通过Dump view UI hierarchy for Automatorfen 分析,得到结构如图:
通过代码实现
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="48dp"
android:gravity="center"
android:text="标题1" />
<EditText
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"
android:text="save" />
<Button
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"
android:text="cancel" />
</LinearLayout>
</LinearLayout>
有了初步的认识,就来看看经常会遇到情景
example2:
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="textview1"
android:background="#ee0000"
android:layout_weight="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="textview2"
android:background="#eeee00"
android:layout_weight="2"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="textview3"
android:background="#ee0ee0"
android:layout_weight="2"/>
example3:
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="textview1"
android:background="#ee0000"
android:layout_weight="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="textview2"
android:background="#eeee00"
android:layout_weight="2"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="textview3"
android:background="#ee0ee0"
android:layout_weight="2"/>
其中,example2与example3区别
<TextView
android:layout_width="xxx"
...
/>
分析可以知道layout_weight表示:控件长度= 原来长度(Length) + 屏幕剩余(SLength)* weight_i,其中屏幕长度为L
example2中:
Length = 0,
SLength= L,
textview1.weight = 1/5
textview1.length = 1/5L;
同理 textview2.length = 2/5L
textview3.length = 2/5L
example3中:
Length= L,
SLength = -2L,
textview1.length = L + (- 2/5 L) = 3/5L
textview2.length = L + (- 4/5 L) = 1/5L
textview3.length = L + (- 4/5 L) = 1/5L
ps:
textview1.weight =1
textview2.weight =2
textview3.weight =3
如果这样设置layout_weight的值,当 android:layout_width="match_parent"的情景下,
你会发现
textview1.length = L + (- 2/6 L) = 2/3L
textview2.length = L + (- 4/6 L) = 1/3L
textview3 不见啦啦啦
Android中layout_weight的属性理解的更多相关文章
- 【转】Android:Layout_weight的深刻理解
原文网址:http://mobile.51cto.com/abased-375428.htm 本文详细介绍了Android布局中Layout_weight的属性,它是用来分配属于空间的一个属性,你可以 ...
- Android中的windowSoftInputMode属性详解
这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下 在前面的一篇文章中 ...
- Android:LinearLayout布局中Layout_weight的深刻理解
首先看一下LinearLayout布局中Layout_weight属性的作用:它是用来分配属于空间的一个属性,你可以设置他的权重.很多人不知道剩余空间是个什么概念,下面我先来说说剩余空间. 看下面代码 ...
- android中布局文件中 layout_weight 的属性详解
在不同的情况下,layout_weight属性作用是不同的.主要有两种属性: 1.当布局中的控件的尺寸(宽和高)都有指定时,它所表示的该控件在父容器中的比重,及它在父容器中所占的比例,数值越大,比重越 ...
- Android中Edittext的属性
//此为转载别人的,挺不错的 1.EditText输入的文字为密码形式的设置 (1)通过.xml里设置: 把该EditText设为:android:password="true" ...
- Android:Layout_weight的深刻理解
最近写Demo,突然发现了Layout_weight这个属性,发现网上有很多关于这个属性的有意思的讨论,可是找了好多资料都没有找到一个能够说的清楚的,于是自己结合网上资料研究了一下,终于迎刃而解,写出 ...
- [转]Android:Layout_weight的深刻理解
http://mobile.51cto.com/abased-375428.htm 最近写Demo,突然发现了Layout_weight这个属性,发现网上有很多关于这个属性的有意思的讨论,可是找了好多 ...
- android中xml tools属性详解
第一部分 安卓开发中,在写布局代码的时候,ide可以看到布局的预览效果. 但是有些效果则必须在运行之后才能看见,比如这种情况:TextView在xml中没有设置任何字符,而是在activity中设置了 ...
- android中xmlns:tools属性详解
今天读到一篇总结的非常棒的文章,写的逻辑很清晰也很实用,很少见到如此棒的文章了.就原文转发过来,我把格式给整理了一下,分享给园子里的各位朋友!好久没写博客了,就为2015年的11月留份纪念吧.希望对你 ...
随机推荐
- bzoj1503 [NOI2004]郁闷的出纳员(名次树+懒惰标记)
1503: [NOI2004]郁闷的出纳员 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 8705 Solved: 3027[Submit][Statu ...
- dojox.grid.EnhancedGrid 和 dojox.grid.DataGrid 的继承关系
dojox.grid.EnhancedGrid 的介绍说, EnhancedGrid 是基于 DataGrid 提供增强功能的. EnhancedGrid (dojox.grid.EnhancedG ...
- HDU2050(分平面问题)
分平面问题: 一.n条直线最多分平面问题. n条直线最多可以把平面分成多少个区域? 此类问题主要采用递归的思想.当有n-1条直线时,平面最多被分成了f(n-1)块区域.如果要使第n条直线分的区域最多, ...
- 修改上一篇文章的node.js代码,支持默认页及支持中文
服务端 app.js var app = require('http').createServer(handler) var io = require('socket.io')(app); var f ...
- Java多线程编程(二)
在 Java多线程编程(一) 中的多线程并没有返回值,本文将介绍带返回值的多线程. 要想有返回值,则需要实现新的接口Callable而不再是Runnable接口,实现的方法也改为call()方法,执行 ...
- [Redux] Writing a Todo List Reducer (Adding a Todo)
Learn how to implement adding a todo in a todo list application reducer. let todo = (state = [], act ...
- Cocos2d-x 3.x 头像选择,本地相册图片+图片编辑(Android、IOS双平台)
大连游戏产业不是很发达,最后,选择一个应用程序外包公司.积累的工作和学习过程中的一点业余生活微信体验,我想分享的游戏小朋友的爱. 在应用开发过程中会经常实用户上传头像的功能,在网上找了N多资料发现没有 ...
- Two-Phase-Commit for Distributed In-Memory Caches--reference
Part I reference from:http://gridgain.blogspot.kr/2014/09/two-phase-commit-for-distributed-in.html 2 ...
- 27个Jupyter Notebook使用技巧及快捷键(翻译版)
Jupyter Notebook Jupyter Notebook 以前被称为IPython notebook.Jupyter Notebook是一款能集各种分析包括代码.图片.注释.公式及自己画的图 ...
- karma、jasmine做angularjs单元测试
引用文:karma.jasmine做angularjs单元测试 karma和jasmine介绍 <1>技术介绍 karma karma是Testacular的新名字 karma是用来自动化 ...