Android中的layout_weight属性
2017-11-01 10:05
543 查看
前言
之前用到layout_weight属性,对于为什么要把layout_width or layou_height 设置为0dp不得其解。今天偶然看到一篇文章说了这一点,所以在此记录一下, 同时借助博客告诉更多人,如若侵权,请立即联系我!我马上删除。介绍
layout_weight,顾名思义,是重量、权重的意思,代表这个控件的权重或者说在空间中所占的重要性有多大。它用于对父布局剩余空间的进行划分,比如控件1的权重为2,控件2的权重为1,则理论上控件1占剩余空间的3分之2,而控件2占剩余空间的3分之1。举例(注意这里我们先按标准写法来):<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout>
根据上图看,确实我们的按钮控件分割了父布局width的空间,且为3:2,那我们改为wrap_content呢,会是什么效果呢?修改代码为:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout>
我们会发现没变啊,这不是可以用wrap_content吗?注意wrap_content的作用为:The view should be enough big to enclose its content。就是说设置这个属性的控件所占的空间必须足够大以包含它所有具有的content,这里因为我们的content为空,所以可以看做layout_width = 0,效果与上等同。但如若你的控件有content,将不再是这个样子,代码如下(为了对比效果,这里我让两个控件的权重比为1:1):
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="55555" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="666666666666666666666666666" /> </LinearLayout>
这里可以看到两个控件竟然不是按照1比1来划分空间,难道不应该是两个控件各占一半吗?layout_weight失效了吗?其实并没有失效,只是我们要修正上面的定义:它用于对父布局剩余空间的进行划分,修正为应用于对父布局剩余空闲空间的进行划分。所以上面那个图对应的是父布局的长度=button1的content长度+2分之1剩余空闲空间+button2的content长度+2分之1剩余空闲空间。
也就是说:
2分之1剩余空闲空间 = (父布局长度 - button1的content长度 - button2的content长度)/ 2;因为我们设置了wrap_content,所以在分配时,会优先为控件分配这些空间,之后再把剩余空间按权重再分配。如何切掉button1和button2的初始所占空间呢,很明显我们只要设置layout_width=0来告诉android我这个控件无需预留空间,请直接按权重分配。试验一下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="55555" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="666666666666666666666666666" /> </LinearLayout>
果然,当我们设置预留空间为0时,就可以按照真正的权重来划分了,可以看到因为按钮2的内容太多了,横向无法发展,只好纵向发展了(因为你设置了wrap_content,若固定一个高度,则无法纵向发展,只能丢弃后面的内容)。
注意:layout_weight是不用在relativelayout中,想想就知道,相对布局是规定内含控件之间的相对位置,可以类比网页的浮动效果,它把控件从文档流删除了,本身不占空间(这样表述不太正确)。
相关文章推荐
- android布局中的layout_weight属性
- Android布局中Layout_weight的属性
- 【layout_weight权重】Android 对Layout_weight属性完全解析以及使用ListView来实现表格
- Android LinearLayout的android:layout_weight属性
- android:layout_weight属性详解
- Android布局中Layout_weight的属性
- Android视图控件属性layout_weight的作用
- android中如何在代码中直接设置View的layout_weight属性
- 【layout_weight权重】Android 对Layout_weight属性完全解析以及使用ListView来实现表格
- Android 布局属性layout_weight不起作用?
- Android 布局属性 Android:layout_weight 总结
- 50个Android开发技巧(01 好好利用layout_weight属性)
- 使用android:layout_weight属性来给view group中各个控件设置所占空间大小比重
- android:layout_weight 属性
- 50个Android开发技巧(01 好好利用layout_weight属性)
- 对android的layout_weight属性的理解
- android布局元素的layout_weight属性
- Android百分比布局之layout_weight属性
- Android中layout_weight的属性理解
- android:layout_weight属性详解