Android LinearLayout layout_weight的几点解释

本文详细解析了Android LinearLayout布局中子控件的权重参数android:layout_weight的工作原理。通过数学公式阐述了不同情况下权重值如何影响控件尺寸,并解释了两种常见误解的来源。

http://www.2cto.com/kf/201402/280721.html  原作地址:



关于Android开发中的LinearLayout子控件权重android:layout_weigh参数的作用,网上关于其用法有两种截然相反说法:


说法一:值越大,重要性越高,所占用的空间越大;

说法二:值越大,重要性越低,所占用的空间越小。

到底哪个正确?哪个错误?抑或还有其他解释?请点击查看关于weight 权重参数作用的详分析:


其实这两种情况都不太准确;

准确的解释是,weight 权限 是用于分配父控件某一方向上尺寸-所有子控件在该方向上设定尺寸和 所得值的一个参数,把这个相减得到的结果(可能为正也可能为负)按照某个子控件weight值占所有weight值的比例分配给子控件,子控件在该方向上的最终实际尺寸为(控件设定尺寸+weight权限分配尺寸(可能为负))。


例如竖直方向的LinearLayout控件F中,两个控件分别为:

A控件 hight = a, weight =w_a;

B控件 hight = b,weight=w_b;

父控件F 实际 hight = c;


则控件A和控件B的最终实际尺寸为:

A控件实际hight_a =a+(c-(a+b))*w_a/(w_a+w_b) ;

B控件实际hight_b =b+(c-(a+b))*w_b/(w_a+w_b) ;


若:w_a= 1; w_b= 2;

则:

hight_a=a+(c-(a+b))/3;

hight_b=a+(c-(a+b))*2/3;


所以 当(c-(a+b))值大于0时,也就是所有子控件高度(或宽度)之和小于父控件高度(或宽度)时,weight权限值越大会使得控件尺寸在原设定尺寸基础上增加的值越大;

所以 当(c-(a+b))值小于0时,也就是所有子控件高度(或宽度)之和大于父控件高度(或宽度)时,weight权限值越大反而会使得控件尺寸在原尺寸基础上减去的值更大;

但控件的实际尺寸是按上面公式计算结果得出,控件之间并不是在所有情况下都是weight值越大控件所占空间越大或weight值越小控件所占空间越小。

那么为什么一般大家都这么认为呢?其实是有两种特殊情况:

一是:所有控件该方向尺寸都设定为0dp,这时 公式中的a=0;b=0;最终A、B控件的尺寸分别为:

hight_a=c*w_a/(w_a+w_b);

hight_b=c*w_b/(w_a+w_b);

这就是所谓的说法一:值越大,重要性越高,所占用的空间越大。

二是:所有控件该方向尺寸都设定为fillparent,这时 公式中的a=c;b=c;最终A、B控件的尺寸分别为:

hight_a=c*(1-*w_a/(w_a+w_b));

hight_b=c*(1-*w_b/(w_a+w_b));

这就是所谓的说法二:值越大,重要性越高,所占用的空间越小。

至此,关于LinearLayout布局中的控件 的weight值的的作用,应该有了一个比较正确的认识了:

尺寸是通过公式计算出来的,只有在特殊情况下才会是普遍认为的:值越 空间越大或值越大占用空间越小。


<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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值