android中的weight
android:layout_weight详细分析介绍:
布局文件是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button1"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Button2"
/>
</LinearLayout>
出现的布局是:button1占了2/3,button2占了1/3。
但是如果将布局文件中的button的属性android:layout_width="fill_parent"改为android:layout_width="wrap_content"那么出现的结果为:button1占了1/3,button2占了2/3。
出现这样的结局是什么意思呢?下面是详细解释:
linearLayout中包含有weight的child时,linearLayout会measure两次:
设屏幕宽度为X
第一次:button1的measuredWidth为X,button2也为X (因为用了weight,所以linearLayout每次measure child时不考虑前一个已经占用的大小),total_width为2X
第二次:计算delta=x-total_width=-x,然后会将button1的宽度设为
x+delta*1/3=0.66x, button2的宽度为 x+delta*2/3=0.33x
那我现在对这句话重新概括一下:“因为设置了button1的权重最小,所以它占用的布局优先级就越高”,也就是说layout_weight越小,布局优先级越高。也许在Android里面布局并没有优先级之说,我这里只是为了说明问题,自己定义的,所以朋友们不要拍砖。
那首先分析一下当layout_width属性设置为fill_parent的时候,即充满父布局,当然意思是这个控件要根据weight的设置尽可能的大,因此,依上例而论,button1的weight设为1,button2的weight设置为2.即button1的优先级最高,因此,要填充父布局就要button1先来填充,尽可能的大,那这个尽可能又是多少呢,这就要综合layout里其它控件的weight值了,然后做一下运算,button1占据2/3,button2占据1/3.你也可以把button2设置为一个非常大的数,比如2000,此时在Graphical Layout模式下可以看到button1填充满了整个宽度,而看不到button2的影子,事实上button2还是存在的,你把鼠标指向button1的后面就可以看到一个长长的竖条,那个就是button2,已经非常非常小了。因此,在layout_width设置为fill_parent的时候,weight所代表的是你的控件要优先尽可能的大。
接着是当layout_weight设置为wrap_content的时候,即适应内容的宽度,意思是这个控件要尽可能的小,只要能把内容显示出来就可以了,同样的,如果把button1和button2的layout_weight设置为wrap_content后,button1的weight为1,button2的weight为2.那么button1要优先尽可能的小,而button2也要尽可能的小,只是优先级不一样,因为设置了weight,所以这两个控件总的宽度要填满父布局的宽度,所以就又要计算每个控件所占据的大小,此时,button1的优先级较高,共有两份,一份1/3,一份2/3,button1要尽可能的小,那button1当然要选1/3,因此,我们看到的效果反而是button2占据的较大。这里要说的是如果把权值同样做如下设置:button1为1,button2为2000,那button1是不是就要占据1/2000的空间呢?这么理解就错了,刚才说了,要尽可能的小,但这个小是有一个限度的,那就是wrap_content,就是还要是内容完完整整的显示出来,同样的,尽可能的大也是有一个限度的,那就是父布局的宽度。因此,在layout_width设置为wrap_content的时候,weight所代表的是你的控件要优先尽可能的小。
所以,要对weight做了解,要深深的理解下面两句话:
在layout_width设置为fill_parent的时候,layout_weight所代表的是你的控件要优先尽可能的大,但这个大是有限度的,即fill_parent.
在layout_width设置为wrap_content的时候,layout_weight所代表的是你的控件要优先尽可能的小,但这个小是有限度的,即wrap_content.
layout_height 同 layout_width.
实际上上面做的解释主要是对fill_parent和wrap_content做的解释,我们可以参考下面的2篇博文知道,上面的解释是存在一定的问题的。
一般来说,我们使用layout_weight,我们可能会使用在各个控件或者布局(像LinearLayout),应用最广泛的就是DV6300-T的主界面的布局,全部用的是layout_weight来控制的,这样可以更好的适应不同的分辨率。
所以我们经常用layout_weight来设置布局,一般是设置不同的LinearLayout为不同的layout_weight来完成基本布局,还可以利用LinearLayout来占据空间都是没有任何显示,这样的就是完全实现比重布局的LinearLayout,最后在各个LinearLayout里面又实现多个控件的layout_weight控制,同样也可以使用只占控件无显示的控件来达到分配布局的效果,DV6300-T就是明显例子。
还需要注意的就是LinearLayout的orientation如果没有设置,那么默认的就是horizontal水平方向。
按比例显示LinearLayout内各个子控件,需设置android:layout_width="0dp",
如果为竖直方向的设置android:layout_height="0dp"。
在这种情况下某子个控件占用LinearLayout的比例为:本控件weight值 / LinearLayout内所有控件的weight值的和。
一般我们控制比重都是设置对应方向的高或宽为0dp来实现我们想的比重控制,而很少用fill_parent或wrap_content,不过我们来理解这2种设置的区别。
还有一句话需要理解就是: 权重(layout_weight)最小,所以它占用的布局优先级就越高。
还需要注意的是保存一致性控制:
如果我们要控制显示比重,那么我们一般要求各个等级的元素的控制方式必须一致,比如多个button控件处于同一等级来控制比重布局,那么如果设置的是android:layout_width="0dp"这样的方式来控制,就必须把所有的等级控件都用android:layout_width="0dp"来控制,而不能有的用android:layout_width="0dp",而还有的用android:layout_width=" fill_parent ",这样就会出现混乱了。
下面还有一个例子:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#00AAC0"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#CCdd00"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#0000FF"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2">
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#FFAA00"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#CCdd00"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#0000FF"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2">
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#00FF00"
/>
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#CCdd00"
/>
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#cc00FF"
/>
</LinearLayout>
</LinearLayout>
----------------------------------------------------------------------------------------------------------------------------------------------------
原帖地址:http://geyubin.iteye.com/blog/1482198
转载于:https://www.cnblogs.com/Rocky_/archive/2012/04/23/2466674.html
android中的weight相关推荐
- Android中measure过程、WRAP_CONTENT详解以及 xml布局文件解析流程浅析
转自:http://www.uml.org.cn/mobiledev/201211221.asp 今天,我着重讲解下如下三个内容: measure过程 WRAP_CONTENT.MATCH_PAREN ...
- Android中measure过程、WRAP_CONTENT详解以及xml布局文件解析流程浅析(下)
本文原创, 转载请注明出处:http://blog.csdn.net/qinjuning 上篇文章<<Android中measure过程.WRAP_CONTENT详解以及xml布局文 ...
- Android中measure过程、WRAP_CONTENT详解以及xml布局文件解析流程浅析(上)
在之前一篇博文中<< Android中View绘制流程以及invalidate()等相关方法分析>> ,简单的阐述 了 Android View 绘制流程的三个步骤,即: 1. ...
- android 界面组件,安卓开发学习周第三篇——Android中的UI组件
原标题:安卓开发学习周第三篇--Android中的UI组件 在Android APP中,所有的用户界面元素都是由View和ViewGroup的对象构成的.View是绘制在屏幕上的用户能与之交互的一个对 ...
- Android中当一行显示两个TextView时,第一个可“…”,而第二个必须显示全部
在Android中当一行显示两个TextView时,第一个可"-",而第二个必须全部显示这种布局时: <RelativeLayout android:layout_width ...
- Android中常见五种布局管理器——RelativeLayout、LinearLayout、FrameLayout、TableLayout、GridLayout
目录 布局管理器 RelativeLayout 常见属性 Relative的实践操作(实现软件更新界面) LinearLayout 常见属性 LinearLayout的实践操作(模范登录以及微信底部) ...
- 从源码解析-Android中View的绘制流程及performTraversals方法
谈谈Activity的setContentView是怎么加载XML视图的 谈谈Activity的View怎么与View绘制工具ViewRootImpl关联的 在前面两篇文章中分析了View是如何跟绘制 ...
- 深入解析Android中View的工作原理
Android中的任何一个布局.任何一个控件其实都是直接或间接继承自View实现的,当然也包括我们在平时开发中所写的各种炫酷的自定义控件了,所以学习View的工作原理对于我们来说显得格外重要,本篇博客 ...
- Android中常用的设计模式一
对于开发来说,设计模式是必不可少的,就像是一个项目的架海紫金梁,而且熟悉常用的设计模式可以有助于我们研读源码.好,废话不多说,开始介绍 1> 单例: 不想再说了,如果还有人不会,我叫他大爷. 大 ...
最新文章
- 有存款,才能过得更踏实
- MATLAB中深度学习的数据集合
- 数据库优化:Mysql慢查询的定位和分析
- Django中配置静态文件路径
- co11n——生产订单确认的BAPI
- Python模拟Linux的Crontab, 写个任务计划需求
- Anaconda安装绘图模块altair
- Mac下Tomcat乱码的问题
- mysql explain desc_MySQL中EXPLAIN结果的参数详解
- Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(
- Windows10添加英文输入法
- 建筑工地使用的人脸识别门禁实名制管理系统是什么
- 华为员工去面试被淘汰后怒怼HR:华为出来的也能被拒,很无语
- 队列 front rear
- Springboot返回数据形式
- 恐怖的ChatGPT!
- 1月第1周业务风控关注 | 四部门联合印发App违法违规收集使用个人信息行为认定方法
- 使用七牛的文档转换服务将PPT转换为JPG
- 自学python面试_Python自学之路-面试题
- python怎么爬虎牙_Python爬虫:爬取虎牙星秀主播图片