android:layout_weight 属性的工作原理

本文解释了Android中LinearLayout的layout_weight属性的工作原理,并通过实例演示如何使用该属性来控制视图的大小分配。

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

android:layout_weight属性 字母上的意思是权重,这常常让人误解为整个空间的划分权重之比。但其实它指的仅仅是剩余空间划分的权重(比如剩余宽度、剩余高度)。而所谓的剩余空间当然就是指的那些没有组件占用的空间了,相当于是总宽度/总高度  -  所有组件的宽度之和/高度之和。


下面举个例子详细说明一下。

我们LinearLayout  中放置两个组件(按钮或者别的什么) ,两个子组件的  android:layout_weight属性设置成1,它告诉LinearLayout如何布置安排子组件。我们已经为两个组件设置了同样的值,但这并不意味着它们在屏幕上占据同样的宽度。在决定子组件视图的宽度时,LinearLayout使用的是layout_width与layout_weight参数的混合值。


LinearLayout是分两个步骤来设置视图宽度的。

第一步,LinearLayout查看layout_width属性值(竖直方位则查看layout_height属性值)。如果组件的layout_width属性值都设置为wrap_content,因此它们获得的空间大小仅够绘制自身。(在预览界面,很难看出layout_weight是如何工作的,因为组件显示大小不一定就是实际显示的大小。)

第二步,LinearLayout依据layout_weight属性值进行额外的空间分配,在布局中,如果两个组件拥有相同的layout_weight属性值,因此它们均分了同样大小的额外空间。相当于:组件总宽度=组件layout_width + 剩余宽度/2。若将组件1的weight值设置为2,那么它将获得2/3的额外空间,组件2则获得剩余的1/3。


weight设置值也可以是浮点数。对于weight设置值,开发者有着各自的使用习惯。在这里,我们使用的是一种cocktail recipe式的weight设置风格。另一种常见的设定方式是各组件属性值加起来等于1.0或100。这样,这个例子中按钮组件的weight值则应该是0.66或66。

如果想让LinearLayout分配完全相同的宽度给各自的视图,该如何处理呢?很简单,只需设置各组件的layout_width 属性值为0dp 以避开第一步的空间分配就可以了。这样

LinearLayout就会只考虑使用layout_weight属性值来完成所需的空间分配。那就1:1 每个组件各分一半了。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/gradient_background"> <!-- 标题栏 --> <RelativeLayout android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?android:attr/actionBarSize" android:background="#44bd32"> <Button android:id="@+id/fanHui" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginStart="8dp" android:layout_marginEnd="8dp" android:background="@android:color/transparent" android:foreground="?android:attr/selectableItemBackground" android:text="返回" android:textColor="@android:color/white" android:textSize="16sp" /> </RelativeLayout> <!-- 底部按钮 --> <LinearLayout android:id="@+id/footer_buttons" android:layout_width="match_parent" android:layout_height="1dp" android:layout_alignParentBottom="true" android:background="@android:color/white" android:orientation="horizontal"> <!-- <Button--> <!-- android:id="@+id/device_location"--> <!-- android:layout_width="match_parent"--> <!-- android:layout_height="match_parent"--> <!-- android:gravity="center"--> <!-- android:text="设备定位"--> <!-- android:textSize="19sp"--> <!-- android:background="#44bd32"--> <!-- android:textColor="@android:color/white" />--> </LinearLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="380dp" android:layout_above="@id/footer_buttons" android:layout_below="@id/toolbar" android:layout_marginBottom="298dp" android:background="@android:color/black" android:padding="0dp"> <!-- 视频播放器 --> <VideoView android:id="@+id/video_player" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true" /> <!-- 播放控制条 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="#99000000" android:orientation="horizontal" android:padding="8dp"> <ImageButton android:id="@+id/btn_play_pause" android:layout_width="40dp" android:layout_height="40dp" android:background="?attr/selectableItemBackgroundBorderless" android:src="@drawable/ic_play_sel" /> <SeekBar android:id="@+id/video_seekbar" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="center_vertical" /> <TextView android:id="@+id/tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="00:00/00:00" android:textColor="@android:color/white" android:textSize="14sp" /> </LinearLayout> </RelativeLayout> <GridLayout android:id="@+id/thumbnail_grid" android:layout_width="match_parent" android:layout_height="301dp" android:layout_above="@id/footer_buttons" android:layout_below="@id/video_container" android:layout_marginBottom="-1dp" android:background="@android:color/white" android:columnCount="4" android:padding="8dp" android:rowCount="2"> <!-- 动态添加的缩略图将放在这里 --> <!-- 示例缩略图 (可选) --> <ImageView app:layout_columnWeight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="4dp" android:adjustViewBounds="true" android:contentDescription="萤石缩略图" android:src="@drawable/default_thumbnail" /> <!-- 更多缩略图... --> </GridLayout> </RelativeLayout> 依据上述代码解决2个报错:Cannot resolve symbol '@id/video_container' Namespace 'app' is not bound
06-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

编程序的艺术家

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

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

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

打赏作者

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

抵扣说明:

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

余额充值