Android weight运用

本文展示了如何在Android应用中通过XML布局实现网络连接状态的详细显示,并整合了设备信息的直观呈现。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="
http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<Button
android:id="@+id/button_query"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button_query"
android:layout_marginTop="5.0dip"
android:layout_alignParentBottom="true" />

<LinearLayout
android:id="@+id/l1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentTop="true"
android:layout_marginTop="10.0dip">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:text="@string/network_available"
android:layout_weight="1.0" />

<TextView
android:id="@+id/network_available"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:layout_weight="1.0" />
</LinearLayout>

<LinearLayout
android:id="@+id/l2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@id/l1"
android:layout_marginTop="10.0dip">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:text="@string/network_type"
android:layout_weight="1.0" />

<TextView
android:id="@+id/network_type"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:layout_weight="1.0" />
</LinearLayout>

<LinearLayout
android:id="@+id/l3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@id/l2"
android:layout_marginTop="10.0dip">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:text="@string/network_subtype"
android:layout_weight="1.0" />

<TextView
android:id="@+id/network_subtype"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:layout_weight="1.0" />
</LinearLayout>

<LinearLayout
android:id="@+id/l4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@id/l3"
android:layout_marginTop="10.0dip">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:text="@string/network_status"
android:layout_weight="1.0" />

<TextView
android:id="@+id/network_status"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:layout_weight="1.0" />
</LinearLayout>

<LinearLayout
android:id="@+id/l5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@id/l4"
android:layout_marginTop="10.0dip">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:text="网络连接状态(详细超超超)"
android:layout_weight="1.0" />

<TextView
android:id="@+id/network_status_detail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:layout_weight="1.0" />
</LinearLayout>

<LinearLayout
android:id="@+id/l6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@id/l5"
android:layout_marginTop="10.0dip">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:text="@string/network_extra"
android:layout_weight="1.0" />

<TextView
android:id="@+id/network_extra"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:layout_weight="1.0" />
</LinearLayout>

<LinearLayout
android:id="@+id/l7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@id/l6"
android:layout_marginTop="10.0dip">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:text="@string/network_roaming"
android:layout_weight="1.0" />

<TextView
android:id="@+id/network_roaming"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:layout_weight="1.0" />
</LinearLayout>

</RelativeLayout>

### Android Studio 中 `weight` 属性的用法及其属性值 在 Android 开发中,`layout_weight` 是一种非常强大的工具,用于动态分配布局内的可用空间。它主要适用于线性布局 (`LinearLayout`) 并允许开发者按比例分配子视图的空间。 #### `layout_weight` 的基本概念 `layout_weight` 表示某个视图在父容器中的权重值。当多个视图共享同一父容器时,它们会根据各自的权重值来分割剩余空间。默认情况下,如果未显式设置 `layout_width` 或 `layout_height`,则这些值会被视为 `0dp`,从而完全依赖于 `layout_weight` 来决定大小[^1]。 #### 主要属性值解释 - **android:layout_weight**: 它接受浮点数值作为输入参数,表示此特定 View 所占的比例份额。 #### 使用实例分析 ##### 示例 1:水平方向上的权重分布 考虑这样一个场景,在一个水平排列的 `LinearLayout` 下有两个按钮 ButtonA 和 ButtonB ,我们希望这两个按钮占据相同的空间量: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/buttonA" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Button A"/> <Button android:id="@+id/buttonB" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Button B"/> </LinearLayout> ``` 在此配置文件里,两个按钮均设定了相同的权值即 '1' , 而且他们的宽度都被设定成了 "0dp". 结果就是他们平均分享了 LinearLayout 提供的所有横向空间. ##### 示例 2:不均衡的权重分配 如果我们想要让第一个按钮占用更多空间,则可以增加它的权重: ```xml <Button android:id="@+id/buttonC" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:text="Larger Button C"/> <Button android:id="@+id/buttonD" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Smaller Button D"/> ``` 这里 ButtonC 的权重为 '2', 相较之下 ButtonD 只有 '1'. 因此, 在同样的总长度条件下, ButtonC 将获得两倍于 ButtonD 的空间. #### 关键注意事项 - 当使用 `layout_weight` 时,建议将对应的维度 (对于水平布局来说是 width;对于垂直布局则是 height) 设定成 `"0dp"`。这样能确保只有通过权重计算得出的实际尺寸才会生效。 - 不同单位间的转换也可能引起混淆,比如 dp、sp 等等。务必保持一致性以避免意外的结果[^2]。 #### 总结 综上所述,合理运用 `layout_weight` 属性可以帮助创建更加灵活和响应式的用户界面设计。记住总是把关联维数置零,并仔细规划各个组件之间的相对重要程度即可实现预期目标。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值