android:layout_weight的真实含义

本文详细解析了Android中LinearLayout的layout_weight属性。通过实例说明了在不同layout_height设置下,layout_weight如何影响视图的宽度分配。

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

首先声明只有在Linearlayout中,该属性才有效。

android:layout_weight的真实含义是:一旦View设置了该属性(假设有效的情况下),那么该
View的宽度等于原有宽度(android:layout_width)加上剩余空间的占比!

很多人不知道剩余空间是个什么概念,下面 我先来说说剩余空间。
假设手机屏幕的高度为L。

1.当layout_hieght=”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="vertical"
    android:divider="#f40808">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="One"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Two"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Three"/>
</LinearLayout>

上面布局的剩余空间的高度为:
这里写图片描述

系统给上面的3个EditText分配它们的高度值为wrap_content(高度足以包含它们的内容”One”、”Two”、”Three”即可),则剩余空间为:L-3*wrap_content。

2.当layout_hieght=”match_parent”时:
上面三个EditText的每个高度为 L,则剩余空间的高度为L-3*L = -2L。


下面我就来讲,Layout_weight这个属性的真正的意思:
(1)当layout_hieght=”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"
    android:divider="#f40808">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#f20303"
        android:layout_weight="1"
        android:text="One"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#064df2"
        android:layout_weight="2"
        android:text="Two"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#f5f909"
        android:layout_weight="3"
        android:text="Three"/>
</LinearLayout>

效果图:
这里写图片描述

我们假设手机屏幕的宽度为L,则剩余空间的宽度freeWidth为L-3*wrap_content,
这里写图片描述

根据计算公式:
TextView(One)的宽度为:wrap_content+(1/6)*freeWidth.
TextView(Two)的宽度为:wrap_content+(2/6)*freeWidth.
TextView(Three)的宽度为:wrap_content+(3/6)*freeWidth.

2.当layout_hieght=”match_parent”时:
则剩余空间的宽度freeWidth为:L-3*L = -2L;
根据计算公式:
TextView(One)的宽度为:L+(1/6)*freeWidth = (4/6)L.
TextView(Two)的宽度为:L+(2/6)*freeWidth = (2/6)L.
TextView(Three)的宽度为:L+(3/6)*freeWidth = 0L.

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

(3)当没有设置 Layout_weight这个属性时,它默认值是0:

<?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"
    android:divider="#f40808">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#f20303"
        android:text="One"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#064df2"
        android:layout_weight="1"
        android:text="Two"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#f5f909"
        android:text="Three"/>
</LinearLayout>

效果图:
这里写图片描述

因为TextView(One)和TextView(Two)没有设置Layout_weight属性所以默认为0,则将剩余空间全部分配给TextView(Two);

总结:
Google官方推荐,当使用weight属性时,将width设为0dip即可,效果跟设成wrap_content是一样的。这样weight就可以理解为占比了!

参考自:Android:Layout_weight的深刻理解

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/dialog_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="@dimen/dimen_84dp" android:orientation="vertical"> <com.google.android.material.appbar.COUIDividerAppBarLayout android:id="@+id/appbar" style="@style/CommonAppBarStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/color_transparent" android:clickable="true" android:focusable="true" android:paddingLeft="@dimen/dimen_0dp" android:paddingRight="@dimen/dimen_0dp" app:elevation="@dimen/toolbar_elevation"> <com.coui.appcompat.toolbar.COUIToolbar android:id="@+id/toolbar" style="@style/COUIToolBarInAppBarLayoutStyle" android:layout_width="match_parent" android:background="@null" app:supportTitleTextAppearance="@style/textAppearanceSecondTitle" app:titleCenter="false" /> <com.coui.appcompat.tablayout.COUITabLayout android:id="@+id/tab_layout" android:background="@color/color_transparent" style="@style/COUISmallTabLayoutStyle" /> </com.google.android.material.appbar.COUIDividerAppBarLayout> <RelativeLayout android:id="@+id/content_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <com.filemanager.common.view.ViewPagerWrapperForPC android:id="@+id/view_pager_wrapper" android:layout_width="match_parent" android:layout_height="match_parent"> <com.filemanager.common.view.viewpager.RTLViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/appbar" android:clipChildren="false" android:clipToPadding="false" android:orientation="horizontal" /> </com.filemanager.common.view.ViewPagerWrapperForPC> </RelativeLayout> </LinearLayout> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:background="?attr/couiColorBackgroundElevatedWithCard" android:minHeight="@dimen/operation_btn_background_height"> <View android:id="@+id/button_divider" android:layout_width="match_parent" android:layout_height="@dimen/divider_background_height" android:layout_gravity="top" android:alpha="0" android:background="?attr/couiColorDivider" android:forceDarkAllowed="false" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <LinearLayout android:id="@+id/select_root_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginStart="@dimen/dimen_16dp" android:orientation="vertical"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:clickable="false" android:orientation="horizontal"> <TextView android:id="@+id/select_title_content" android:layout_width="match_parent" android:layout_height="match_parent" android:lineHeight="22dp" android:text="@string/not_selected_file" android:textAppearance="@style/couiTextHeadlineXS" android:textColor="?attr/couiColorLabelPrimary" android:textSize="16sp" android:visibility="visible" /> <ImageView android:id="@+id/select_arraw_up" android:layout_width="18dp" android:layout_height="18dp" android:src="@drawable/arrow_up" android:layout_marginLeft="@dimen/dimen_6dp" android:visibility="gone" android:layout_gravity="center"/> </LinearLayout> <TextView android:id="@+id/select_body_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:lineHeight="20dp" android:textSize="14sp" android:textAppearance="?attr/couiTextBodyXS" android:textColor="?attr/couiColorLabelSecondary" android:visibility="gone" android:text="@string/selected_size"/> </LinearLayout> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1"/> <com.coui.appcompat.button.COUIButton android:id="@+id/btn_add_file" style="@style/Widget.COUI.Button.Large" android:layout_width="@dimen/dimen_96dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginEnd="@dimen/dimen_16dp" android:text="@string/label_add_recent_file" /> </LinearLayout> </FrameLayout> </FrameLayout>这是这个页面
最新发布
07-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大数据AI

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值