LinearLayout中的weight属性的计算

来源:互联网 发布:阿里云域名过户步骤 编辑:程序博客网 时间:2024/06/05 15:57

最终宽度 = 控件原来宽度 + 控件在父控件剩余空间所占的百分比(也就是weight的值)

以下两个例子进行说明:
条件:假设父控件宽度为L

<?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"    android:orientation="horizontal" >    <Button         android:layout_height="wrap_content"        android:layout_width="match_parent"        android:layout_weight="1"/>    <Button         android:layout_height="wrap_content"        android:layout_width="match_parent"        android:layout_weight="2"/></LinearLayout>

因为Button1和Button2的layout_width=match_parent, 所以原有宽度为L,而父控件的剩余 宽度就是L - 2L(父控件的宽度 - Button1和Button2的原有宽度);

Button1的宽度: 实际宽度 = L + (L-2L) * 1/3 = (2/3)L;
Button2的宽度: 实际宽度 = L + (L-2L) * 2/3 = (1/3)L;

match_parent可理解为当前控件的宽度=父控件的宽度,而wrap_content可理解为当前控件的宽度=0

height的计算和width一样,不过需要注意设置height是LinearLayout的orientation属性的值为vertical

0 0
原创粉丝点击