50个Android开发技巧(1,使用android:layout_weight和android:weightSum属性的使用)

本文介绍如何利用LinearLayout的weightSum属性与子视图的layout_weight属性结合使用,使按钮在不同屏幕尺寸下居中显示并占据父视图50%的宽度。

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

在给开发者做演讲时, 当我解释如何通过XML文件创建视图的时候, 一个开发者问道: “如果我想将按钮居中显示, 并且占据其父视图宽度的一半, 应该怎么做呢? ” 起初, 我并没有完全理解他的意思, 后来他把想要实现的功能画在了 黑板上, 我才恍然大悟。 他想实现的功能如图1 -1和图1 -2 所示。

看 起来 很 简 单是 吗? 现在 开 始, 请读 者 用5分钟 时 间 实 现这 个 功 能。 在 这 个Hack 里, 我 们 分 析 如 何 结 合 LinearLayout
android:weightSum 属 性 和 LinearLayout 的 子 视 图 的 android:layout_weight 属 性来解决这个问题。 这听起来似乎很简单, 不过
我经常在面试中问到这个问题, 很少有面试者知道最佳答案。



1 -2 居中显示按钮, 并占据父视图 50% 宽度(竖屏)
1 .1 合用 weightSum 属性和 layout_weight 属性
不同 Android 设备的尺寸往往是不同的。 作为开发者, 我们需要创建适用于不同尺寸屏幕的 XML 文件。 硬编码是不可取的, 因此需要其他方法来组织视图。本节分析如何合用 layout_weight weightSum 这两个属 性来
填充布局内 部的任意剩余空间。 android:weightSum(见 1 .3 节) 的开发文档里的一段描述与我们现在想要实现的功能类似, 文档内容
如下:
“定义 weight 总和的 最大值。 如果未指 定该值, 以 所有子 视图 的 layout_weight 属 性的 累 加 值作 为 总 和 的 最大值。 一 个典型的 案例 是: 通过指 定子 视图 的 layout_weight 属 性为 0.5, 并设置LinearLayout weightSum 属性为 1 .0, 实现子视图 占 据可用 宽度50%。”
设想一个场景: 我们要在盒子里放置其他物体。 盒子可用空间 的比例就是 weightSum, 盒子中 每个物体可用空间 的比例就是layout_weight。 例如, 盒子的 WeightSum 1, 我们需要往盒子里放置两个物体: 物体 A 和物体 B。 物体 A layout_weight 0.25物体 B layout_weight 0.75。 那么, 物体 A 可以占据盒子 25%的空间, 而物体 B 可以占据剩下的 75% 的空间。
本章开头所讨论问题的解决方案是与之类似的。 我们为父视图
指定一个 weightSum, 然后指定 Button android:layout_weight 性为 weightSum 的一半。 XML 文件的源码如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
Licensed to Kimberly Brabec <brabec.kimberly.waterside@gmail.com>
Using lazy loading and avoiding replication 3
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="1">
B Reads the
android:weightSum attribute

<Button
android:layout_width="0dp"
C Decides the
button’s width

android:layout_height="wrap_content"
android:layout_weight="0.5"
D
Makes sure it uses exactly
android:text="Click me"/> 50% of available space
</LinearLayout>

 



另外参考文章:http://blog.youkuaiyun.com/zzf112/article/details/7034412

http://blog.youkuaiyun.com/yanzi1225627/article/details/24667299



<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="100dp"> <LinearLayout android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:textSize="16sp"/> <TextView android:id="@+id/author" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:textColor="@color/gray"/> <TextView android:id="@+id/time" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:textColor="@color/gray"/> <TextView android:id="@+id/need" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:textColor="@color/gray"/> </LinearLayout> <com.google.android.material.button.MaterialButton android:layout_width="60dp" android:layout_height="100dp" android:layout_marginHorizontal="12dp" app:cornerRadius="6dp" android:textSize="20sp" android:text="报名" android:textStyle="bold"/> </LinearLayout> 如何解决Indicates how much of the extra space in the LinearLayout is allocated to the view associated with these LayoutParams. Specify 0 if the view should not be stretched. Otherwise the extra pixels will be pro-rated among all views whose weight is greater than 0.
03-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值