LinearLayout的layout_weight的使用

LinearLayout的layout_weight的使用

1.LinearLayout内的控件的layout_width设置为”wrap_content”

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#ff0000"
        android:gravity="center"
        android:text="1"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:background="#00ff00"
        android:gravity="center"
        android:text="2"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:background="#0000ff"
        android:gravity="center"
        android:text="3"/>
</LinearLayout>

效果如下:
这里写图片描述

可以看到这三个TextView是按照1:2:3的比例进行显示的。
问题:
TextView的文本较长,TextView的宽度就会增加。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#ff0000"
        android:gravity="center"
        android:text="111111111111111111"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:background="#00ff00"
        android:gravity="center"
        android:text="2"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:background="#0000ff"
        android:gravity="center"
        android:text="3"/>
</LinearLayout>

效果如下:
这里写图片描述

解决方法
将控件的android:layout_width由”wrap_content”修改为”0dp”:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#ff0000"
        android:gravity="center"
        android:text="111111111111111111"/>
    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:background="#00ff00"
        android:gravity="center"
        android:text="2"/>
    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:background="#0000ff"
        android:gravity="center"
        android:text="3"/>
</LinearLayout>

效果如下:
这里写图片描述

2.LinearLayout内的控件的layout_width设置为”match_parent”

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#ff0000"
        android:gravity="center"
        android:text="1"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:background="#00ff00"
        android:gravity="center"
        android:text="2"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:background="#0000ff"
        android:gravity="center"
        android:text="3"/>
</LinearLayout>

效果如下:
这里写图片描述

第三个TextView丢掉了。
把weight分别改为2,2,3,效果如下:
这里写图片描述

效果让人很困惑。

正确方法:
按比例显示LinearLayout内各个子控件,水平方向的设置android:layout_width=”0dp”,竖直方向的设置android:layout_height=”0dp”。在这种情况下某子个控件占用LinearLayout的比例为:本控件weight值 / LinearLayout内所有控件的weight值的和。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值