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

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

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

1、如果LinearLayout在其子组件相应排列方向上的大小值(layout_width/height)为wrap_content,则忽略所有子组件的layout_weight,且相应方向上的大小值也替换为wrap_content。例如:
<?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_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将被应用:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns: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 属性,此时,其后的所有子组件将保持原始的尺寸。

根据本条特性,解决上面的问题很容易了,见代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns: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
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
                widthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
详见我的另一篇文章,对测量(measure)的详细解说。

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Android 设置界面的布局源代码,其中包括一个 ListView 和一些简单的布局元素: ```xml <?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="vertical"> <!-- 标题 --> <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Settings" android:textSize="24sp" android:padding="16dp"/> <!-- 列表 --> <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:divider="@null" android:dividerHeight="0dp"/> <!-- 底部按钮 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="16dp"> <Button android:id="@+id/save_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Save"/> <Button android:id="@+id/cancel_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Cancel"/> </LinearLayout> </LinearLayout> ``` 你可以将这个布局文件命名为 `app_set_activity.xml`,并将其放置在你的 Android 项目的 `res/layout` 目录中。然后,你可以在代码中使用以下代码来加载这个布局: ```java public class AppSetActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.app_set_activity); // TODO: 在这里添加你的代码 } } ``` 注意,这个布局只是一个示例,你可以根据你的需要进行更改和定制。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值