Android UI杂谈---layout_weight

本文详细探讨了Android中LinearLayout的layout_weight属性在不同设置下的表现。通过三个实例对比了当子视图宽度设置为0dp、match_parent及wrap_content时的表现差异,并分析了源码解释其行为。最后提供了一个实用的例子来展示如何防止子视图被挤压。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对Android 中 LinearLayout中属性layout_weight讨论:
(一)
(1)当android:orientation=”horizontal”android:layout_width=”0dp”时:

<?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" >

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#C82D25"
        android:text="test activity1" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:background="#EBF0F3"
        android:text="test activity2" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:background="#F2CACA"
        android:text="test activity3" />

</LinearLayout>

表现:
成正比
(2)当
android:orientation=”horizontal”android:layout_width=”match_parent”时:

<?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" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#C82D25"
        android:text="test activity1" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:background="#EBF0F3"
        android:text="test activity2" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:background="#F2CACA"
        android:text="test activity3" />

</LinearLayout>

表现:这里写图片描述

(3)当
android:orientation=”horizontal”且 android:layout_width=”wrap_content”时:

<?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" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#C82D25"
        android:text="test activity1" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:background="#EBF0F3"
        android:text="test activity2" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:background="#F2CACA"
        android:text="test activity3" />

</LinearLayout>

表现:
这里写图片描述
总结:
表现最佳的是第一种,第二种把第三个TextView挤掉了,第三种没有按比例排列,完全失去了layout_weight属性的作用。
从源码角度分析:
google LinearLayout源码:


            if (widthMode == MeasureSpec.EXACTLY && lp.width == 0 && lp.weight > 0) {
                // Optimization: don't bother measuring children who are going to use
                // leftover space. These views will get measured again down below if
                // there is any leftover space.
                if (isExactly) {
                    mTotalLength += lp.leftMargin + lp.rightMargin;
                } else {
                    final int totalLength = mTotalLength;
                    mTotalLength = Math.max(totalLength, totalLength +
                            lp.leftMargin + lp.rightMargin);
                }

                // Baseline alignment requires to measure widgets to obtain the
                // baseline offset (in particular for TextViews). The following
                // defeats the optimization mentioned above. Allow the child to
                // use as much space as it wants because we can shrink things
                // later (and re-measure).
                if (baselineAligned) {
                    final int freeWidthSpec = MeasureSpec.makeSafeMeasureSpec(
                            MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.UNSPECIFIED);
                    final int freeHeightSpec = MeasureSpec.makeSafeMeasureSpec(
                            MeasureSpec.getSize(heightMeasureSpec), MeasureSpec.UNSPECIFIED);
                    child.measure(freeWidthSpec, freeHeightSpec);
                } else {
                    skippedMeasure = true;
                }
            }

例子:
如果有这样的需求:
这里写图片描述
右边的箭头不想因为左边的字内容太多被挤掉可以这样写:

<LinearLayout
            android:id="@+id/select_deal_card_list_click"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="13dp"
               android:background="@drawable/selector_card_list_bg_end"
            android:clickable="true"
            android:orientation="horizontal"
            android:gravity="center"
            android:padding="7dp" >

           <TextView
                android:id="@+id/select_deal_card_list_content"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="2111111112222222/卡"
                android:singleLine="true"
                android:ellipsize="end"
                android:layout_weight="1"
                android:textColor="@color/common_gray_txt_bg" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/select_ico" />
        </LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值