关于Android UI组件LinearLayout属性layout_weight与layout_width/height的问题

本文深入解析了Android中LinearLayout的layout_weight属性工作原理,包括其在不同情况下的表现及计算方法,并提供实用技巧。

转自:http://hi.baidu.com/wei_chou/item/04b51be1abb1e316595dd853

在网上搜索了很多关于layout_weight的文章,众说纷纭,且都不准确。后来自己动手测试,通过分析计算得出以下结论:

1、如果LinearLayout在其子组件相应排列方向上的大小值(layout_width/height)为wrap_content,则忽略所有子组件的layout_weight,且相应方向上的大小值也替换为wrap_content。例如:
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android=" http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text="Button01"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="Button02"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="Button03"/>
</LinearLayout>

由于LinearLayout的android:orientation="horizontal",子组件水平排列,而android:layout_width="wrap_content",所有将忽略所有子组件的layout_weight,并将android:layout_width值替换为wrap_content。垂直方向同理。
2、layout_weight值表示该组件应该增加或减少的值占剩余空间的比例,没有优先级之说。至于为什么有的组件会不显示,这不是因为优先级的原因。通过以下公式的计算,你会明白。本人愚钝,拟公式如下:
W=W1+L*P;
其中W表示最终宽度或高度,W1表示第一次测量的宽度/高度,L表示剩余空间的值(可能为负数),P表示根据android:layout_weight属性值计算出来的百分比。
将上面代码更改如下(注意android:orientation跟android:layout_width的对应关系),此时子组件的layout_width/height和layout_weight将被应用:
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android=" http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="Button01"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Button02"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="Button03"/>
</LinearLayout>
android对组件进行布局的时候会组件大小进行两次计算:第一次是测量没有应用android:layout_weight时的值,第二次是根据第一次测量值重新计算组件应用android:layout_weight之后的实际值。
设LinearLayout父组件宽度为w,由于三个Button的layout_width值都为wrap_content,为简单起见,设第一次测量三个Button的宽度都为W1=w/5,合起来的总宽度为total_w=3w/5。则剩余空间的大小为L=w-total_w=2w/5,下面进行第二次计算,分别对三个Button的实际大小进行计算:
Button01:
P=3/(3+2+5)=0.3;
W1=w/5=0.2w;
W=W1+L*P=w/5+2w/5*0.3=0.32w;//增加了0.12w

Button02:
P=2/(3+2+5)=0.2;
W1=w/5=0.2w;
W=W1+L*P=w/5+2w/5*0.2=0.28w;//增加了0.08w

Button03:
P=5/(3+2+5)=0.5;
W1=w/5=0.2w;
W=W1+L*P=w/5+2w/5*0.5=0.4w;//增加了0.2w
结果显而易见。
那如果三个Button的layout_width值都为fill_parent会怎样呢?此时
W1=w;
L=w-total_w=w-3*W1=-2w;
Button01:
P=3/(3+2+5)=0.3;
W1=w;
W=W1+L*P=w+(-2w)*0.3=0.4w;//减少了0.6w

Button02:
P=2/(3+2+5)=0.2;
W1=w;
W=W1+L*P=w+(-2w)*0.2=0.6w;//减少了0.6w

Button03:
P=5/(3+2+5)=0.5;
W1=w;
W=W1+L*P=w+(-2w)*0.5=0;//宽度为0,所以不显示
由于Button03的宽度为0,所以这时只能显示Button01和Button02。而不是因为其layout_weight值比较大,优先级低导致的。
到这里是不是突然觉得很简单啊!
如果Button03的layout_weight改为7呢?自己去算吧,W将小于0,因此也不会显示。
还有一种情况,如果设Button02的layout_width="wrap_content",其他都为fill_parent呢?计算方法一样。
垂直方向同理。
另外当android:orientation="horizontal"时,垂直方向如何应用?首先适用本文第一条,否则当LinearLayout的android:layout_height="fill_parent"时,若子组件的android:layout_height="fill_parent",则纵向填满,否则自适应大小。
补充说明之妙用:

3、若不设置LinearLayout的weightSum以及所有子组件的layout_weight值,即两者的值都为0,则将要出界的组件的尺寸将会被调整。规则如下:所有值为
wrap_content fill_parent 的组件,若按其正常尺寸,有部分在父组件边界内而部分在父组件边界外的,则将其尺寸调整到正好容纳在边界之内;完全在边界外部的组件将会隐藏,即宽或高为0;所有值不为wrap_content fill_parent之外 的组件,即有固定尺寸的,如100dp,则无论在边界内外都按固定尺寸显示。
问题:我不想给子组件设置固定的尺寸,也不想出界的组件会隐藏,而是想当我滚动内容的时候scrollTo(x,y)/scrollBy(x,y),出界的部分会显示出来而不是依然隐藏。根据前面的第1、2条,我们知道应用weight可以让组件出界,但却会改变组件的大小。其实有简单的办法,见下一条。
4、若LinearLayout的weightSum和子组件的layout_weight有一个不为0,当依次应用子组件的layout_weight来进行计算组件尺寸的时候(见第2条),当某子组件与其前面的所有子组件的layout_weight值之和正好等于weightSum的时候,则忽略其后所有子组件的layout_weight属性,此时,其后的所有子组件将保持原始的尺寸。

根据本条特性,解决上面的问题很容易了,见代码:
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android=" http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<View
android:layout_width="0px"
android:layout_height="0px"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="400dp"
android:text="按钮0"/>
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="按钮1"/>
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="按钮2"/>
</LinearLayout>
该布局中,由于View的android:layout_weight正好等于android:weightSum(这里省略了,因为android:weightSum默认为所有子组件的android:layout_weight的值之和),所有的Button都会保持其原始的大小并出界。
读者可以自行测试其他情况,将android:weightSum和各子组件的android:layout_weight设置不同的值,看看效果。
3、4条为后来补充。到这里算是完善了。
补充:更为简单且规范的实现组件出界的方法:

重写onMeasure()方法,调整参数,将测量模式设置为MeasureSpec.UNSPECIFIED
@Override
protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){
widthMeasureSpec=MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED);
super.onMeasure(widthMeasureSpec,heightMeasureSpec);
}
详见我的另一篇文章,对测量(measure)的详细解说。

<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" app:behavior_peekHeight="130dp" app:layout_behavior="@string/bottom_sheet_behavior"> <ScrollView android:layout_width="wrap_content" android:layout_height="wrap_content"> <LinearLayout android:id="@+id/ll_bottom_sheet" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:id="@+id/pull" android:layout_width="match_parent" android:layout_height="30dp" android:background="@color/white" android:clickable="false" android:focusable="false" android:gravity="center" android:paddingTop="15dp" android:src="@drawable/pullselector" /> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp" android:layout_gravity="center_horizontal" android:background="@color/white" android:paddingStart="10dp" android:paddingTop="10dp" android:paddingEnd="10dp" android:weightSum="4"> <ImageButton android:id="@+id/button_mode" android:layout_width="0dp" android:layout_height="60dp" android:layout_weight="1" android:background="@android:color/transparent" android:scaleType="centerInside" android:src="@drawable/mode" /> <ImageButton android:id="@+id/button_schedule" android:layout_width="0dp" android:layout_height="60dp" android:layout_weight="1" android:background="@android:color/transparent" android:scaleType="centerInside" android:src="@drawable/schedule" /> <ImageButton android:id="@+id/button_away" android:layout_width="0dp" android:layout_height="60dp" android:layout_weight="1" android:background="@android:color/transparent" android:scaleType="centerInside" android:src="@drawable/away" /> <ImageButton android:id="@+id/button_timer" android:layout_width="0dp" android:layout_height="60dp" android:layout_weight="1" android:background="@android:color/transparent" android:scaleType="centerInside" android:src="@drawable/timer" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="42dp" android:layout_gravity="center_horizontal" android:background="@color/white" android:paddingStart="10dp" android:paddingEnd="10dp" android:weightSum="4"> <TextView android:layout_width="0dp" android:layout_height="42dp" android:layout_weight="1" android:gravity="center" android:text="Mode" /> <TextView android:layout_width="0dp" android:layout_height="42dp" android:layout_weight="1" android:gravity="center" android:text="Schedule" /> <TextView android:layout_width="0dp" android:layout_height="42dp" android:layout_weight="1" android:gravity="center" android:text="Away" /> <TextView android:layout_width="0dp" android:layout_height="42dp" android:layout_weight="1" android:gravity="center" android:text="Timer" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="10.5dp" android:background="@color/background_gray" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white"> <TextView android:layout_width="113dp" android:layout_height="41dp" android:gravity="center" android:text="Today" android:textColor="@color/text_gray" android:textSize="13dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="70dp" android:background="@color/white" android:paddingStart="35dp" android:paddingEnd="35dp" android:weightSum="3"> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@color/white" android:orientation="vertical"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="12.1" android:textSize="23dp" android:textStyle="bold" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="4dp" android:layout_weight="1" android:text="h" android:textSize="12dp" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top" android:text="Runtime" android:textColor="@color/text_gray" android:textSize="14dp" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@color/white" android:orientation="vertical"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="125.1" android:textSize="23dp" android:textStyle="bold" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="4dp" android:layout_weight="1" android:text="Kmh" android:textSize="12dp" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top" android:text="Usage" android:textColor="@color/text_gray" android:textSize="14dp" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="11dp" android:layout_weight="1" android:background="@color/white" android:orientation="vertical"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="992.3" android:textSize="23dp" android:textStyle="bold" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="4dp" android:layout_weight="1" android:text="Kmh" android:textSize="12dp" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top" android:text="Saving" android:textColor="@color/text_gray" android:textSize="14dp" /> </LinearLayout> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="10.5dp" android:background="@color/background_gray" /> <LinearLayout android:layout_width="match_parent" android:layout_height="56dp" android:background="@color/white" android:gravity="center_vertical" android:paddingLeft="20dp" android:paddingRight="20dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Favorite" android:textSize="16dp" /> <!-- 占位 View,用来推动 ImageButton 到右边 --> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" /> <ImageButton android:id="@+id/button_favorite" android:layout_width="42dp" android:layout_height="28dp" android:background="@android:color/transparent" android:scaleType="centerInside" android:src="@drawable/favorite_on" /> </LinearLayout> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white"> <View android:layout_width="320dp" android:layout_height="0.2dp" android:layout_gravity="center_horizontal" android:background="@color/text_gray" /> </FrameLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="56dp" android:background="@color/white" android:gravity="center_vertical" android:paddingLeft="20dp" android:paddingRight="20dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Device Sharing" android:textSize="16dp" /> <!-- 占位 View,用来推动 ImageButton 到右边 --> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" /> <ImageView android:layout_width="16dp" android:layout_height="16dp" android:background="@android:color/transparent" android:scaleType="centerInside" android:src="@drawable/sharing" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2" android:textColor="@color/text_gray" /> <ImageButton android:layout_width="24dp" android:layout_height="24dp" android:background="@android:color/transparent" android:scaleType="centerInside" android:src="@drawable/more" /> </LinearLayout> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white"> <View android:layout_width="320dp" android:layout_height="0.2dp" android:layout_gravity="center_horizontal" android:background="@color/text_gray" /> </FrameLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="56dp" android:background="@color/white" android:gravity="center_vertical" android:paddingLeft="20dp" android:paddingRight="20dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="FAQ & Feedback" android:textSize="16dp" /> <!-- 占位 View,用来推动 ImageButton 到右边 --> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" /> <ImageButton android:layout_width="24dp" android:layout_height="24dp" android:background="@android:color/transparent" android:scaleType="centerInside" android:src="@drawable/more" /> </LinearLayout> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white"> <View android:layout_width="320dp" android:layout_height="0.2dp" android:layout_gravity="center_horizontal" android:background="@color/text_gray" /> </FrameLayout> <View android:layout_width="match_parent" android:layout_height="280dp" android:background="@color/white" /> </LinearLayout> </ScrollView> </LinearLayout>问题出在哪里
08-22
根据上述功能,优化包含边框的悬浮窗布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:clipChildren="true" android:clipToPadding="true" android:orientation="vertical"> <View android:id="@+id/drag_top" android:layout_width="wrap_content" android:layout_height="14dp" android:visibility="gone" android:layout_gravity="top" android:background="@color/milu_orange" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <View android:id="@+id/drag_left" android:layout_width="14dp" android:layout_height="match_parent" android:visibility="gone" android:background="@color/milu_orange" /> <LinearLayout android:id="@+id/content_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:paddingBottom="10dp"> <LinearLayout android:id="@+id/ll_draggable" android:layout_width="25dp" android:layout_height="25dp" android:layout_centerVertical="true" android:gravity="center"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ml_chat_draggable" /> </LinearLayout> <TextView android:layout_width="43dp" android:layout_height="3dp" android:layout_centerInParent="true" android:background="@drawable/botton_yuan_ef_5" /> <LinearLayout android:id="@+id/ll_close" android:layout_width="25dp" android:layout_height="25dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:gravity="center"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ml_bjlb_close" /> </LinearLayout> </RelativeLayout> <androidx.recyclerview.widget.RecyclerView android:id="@+id/rlv_messagr" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="5" android:scrollbars="none" /> <TextView android:layout_width="wrap_content" android:layout_height="0dp" android:background="@drawable/bg_redius_transparent_75" android:paddingHorizontal="15dp" android:layout_weight="1" android:paddingVertical="5dp" android:text="说点什么吧" android:textColorHint="@color/milu_color_99" /> </LinearLayout> <View android:id="@+id/drag_right" android:layout_width="14dp" android:layout_height="match_parent" android:visibility="gone" android:background="@color/milu_orange" /> </LinearLayout> <LinearLayout android:id="@+id/ll_bottom" android:layout_width="match_parent" android:layout_height="14dp" android:visibility="gone" android:layout_gravity="left|bottom" android:orientation="horizontal"> <View android:id="@+id/drag_bottom_left" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@color/milu_orange" /> <View android:id="@+id/drag_bottom_right" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@color/milu_orange" /> </LinearLayout> </LinearLayout>
07-03
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值