layout_weight是LinearLayout布局里一个重要的属性,就像Qt里的stretch一样,把父视图剩余的空间分配给设 置了layout_weight的组件。这个属性可以让LinearLayout里不同的组件分配不同宽度/高度变得非常灵活。Android官网里对 layout_weight如下解释:
LinearLayout also 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. Child views can specify an integer weight value, and then any remaining space in the view group is assigned to children in the proportion of their declared weight. Default weight is zero. For example, if there are three text boxes and two of them declare a weight of 1, while the other is given no weight (0), the third text box 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 boxes are measured. If the third box 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.
大意就是layout_weight的值越大,所占比例也越大。没错,这跟我们平常理解的stretch是一致的,但是如果把它理解成 layout_weight值相同则组件占用宽度或高度一致的话就错了,这里说的是布局完了之后的剩余空间如何分配,layout_weight相同只说 明剩余空间的分配大小相同,而组件的实际宽度/高度则是组件需要的空间加上layout_weight分配的空间。layout_weight的设置和 layout_height,layout_width不同的组合得出来不同的结果!
且先看官网上关于LinearLayout的例子:
<?xml version="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:text="red"
android:gravity="center_horizontal"
android:background="#aa0000"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="green"
android:gravity="center_horizontal"
android:background="#00aa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="blue"
android:gravity="center_horizontal"
android:background="#0000aa"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="yellow"
android:gravity="center_horizontal"
android:background="#aaaa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:text="row one"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:text="row two"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:text="row three"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:text="row four"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
在2.2的模拟器上显示的结果如下:
各颜色条的宽度是不一致的,因为red,green,blue和yellow这几个字符串的长度不一样,如果将几个标签都改成test,则得到:
现在四个颜色条的宽度是相同的了,如果想既保持字符串不一致,又想得到相同宽度的颜色条:
把TextView中的属性android:layout_width值改为"fill_parent"
得到的结果似乎可以满足要求:
再这个基础上red颜色条的layout_weight设置为2。
红色颜色条彻底没了,对此不知道如何解释。如果layout_width是wrap_content的话则是正常的:
注意,这里几个颜色条的占用比例并不是2:1:1:1,原因之前说了。
利用嵌套的LinearLayout可以达到2:1:1:1的显示效果
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_weight="1">
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_weight="3">
<TextView android:text="red" android:gravity="center_horizontal"
android:background="#aa0000" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_weight="2">
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_weight="1">
<TextView android:text="green" android:gravity="center_horizontal"
android:background="#00aa00" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_weight="1">
<TextView android:text="blue" android:gravity="center_horizontal"
android:background="#0000aa" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_weight="1">
<TextView android:text="yellow" android:gravity="center_horizontal"
android:background="#aaaa00" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
再测试LinearLayout之间的layout_weight设置带来的影响,将第一个LinearLayout的layout_weight设置为2之后:
和view里的效果刚好相反,layout_weight大的LinearLayout占得比例最小,刚好成反比。不知道Google为什么要采用这样截然相反的策略。
最后总结一下吧:
1. LinearLayout内部的子控件之间的layout_weight是按照正比例分配空间
2. LinearLayout之间的layout_weight是按照反比例分配空间
3. 在Horizontal的LinearLayout中,控件A和控件B的layout_weight分别设置为2和1,并不代表两者的宽度之比为2:1。 控件的宽度等于空间本身需要的宽度,加上通过layout_weight设置分配到了父空间里的宽度。垂直方向的LinearLayout也同理。
4.要想实现控件A和控件B的宽度严格按比例显示,可以每个控件之上都添加一个LinearLayout,在LinearLayout的属性里设置layout_weight.
相关推荐
为什么有时候像android:layout_marginBottom等变量的赋值为负数? 例如如下代码: android:orientation="vertical" android:id="@id/Widget_2X4_frame" android:layout_width="fill_parent" android:...
android:layout_gravity 属性详细解读
android_layout属性大全,包括layou的各种属性的汉语意思.方便查找对应个属性
今天主要说的是对Layout_weight属性的完全解析,以及利用Layout_weight这个属性使用ListView来实现表格的效果,我们都知道Android里面专门有一个TableLayout来实现表格的,说实话,我平常开发中用TableLayout还是...
点对点 Android_Layout_之_RelativeLayout_代码实现相对布局 使用
查了下资料,说是要设置layout_weight属性 资料说得不是很清楚,也没仔细看,就去弄,结果越弄越混乱。 于是静下心来,自己写xml测试,发现如下。 如果LinearLayout是最外面的一层,它是不会弹出layout_weight属性的...
Android layout_weight使用方法及实例,需要的朋友可以参考一下
android 布局的基本属性中文详细描述。
layout_gravity 在 线性布局 和 帧布局 中的应用 通过代码 直观的展示 layout_gravity 和 gravity 的区别
-<LinearLayout android:background="@drawable/aaa" android:weightSum="1" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android=...
用Eclipse加载项目工程 <LinearLayout xmlns:android=... android:layout_weight="0.66" android:background="@drawable/blue_bg" android:orientation="vertical" > android:layout
View的布局显示方式有下面几种:线性布局(Linear Layout)、相对布局(Relative Layout)、表格布局(Table Layout)、网格视图(Grid View)、标签布局(Tab Layout)、列表视图(List View)、绝对布局...
android编程:界面UI元素介绍、Android中应用界面布局以及常用widget组件
-<LinearLayout android:background="@drawable/aaa" android:weightSum="1" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android=...
Android支持多屏幕机制即用为当前设备屏幕提供一种合适的方式来共同管理并解析应用资源。本文就介绍了4中Android屏幕自适应解决方案。...传统的layout_weight使用方法是将当前控件的layout_width和layout_
android:layout_centerHrizontal 水平居中 android:layout_centerVertical 垂直居中 android:layout_centerInparent 相对于父元素完全居中 android:layout_alignParentBottom 贴紧父元素的下边缘 android:layout_...
NULL 博文链接:https://trylovecatch.iteye.com/blog/1195121
android:layout_width= match_parent android:layout_height= wrap_content > <Button android:layout_width= match_parent android:layout_height= wrap_content android:singleLine= true />...