android layout-weight属性

本文解释了Android中布局权重属性的工作原理,并通过实例展示了如何使用它来优化用户界面布局,特别是当需要灵活分配剩余空间给不同视图元素时。

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

The weight value is a number that specifies the amount of remaining space each view should consume, relative to the amount consumed by sibling views. This works kind of like the amount of ingredients in a drink recipe: “2 parts soda, 1 part syrup” means two-thirds of the drink is soda. For example, if you give one view a weight of 2 and another one a weight of 1, the sum is 3, so the first view fills 2/3 of the remaining space and the second view fills the rest. If you add a third view and give it a weight of 1, then the first view (with weight of 2) now gets 1/2 the remaining space, while the remaining two each get 1/4.

The default weight for all views is 0, so if you specify any weight value greater than 0 to only one view, then that view fills whatever space remains after all views are given the space they require.


以上是android官网给出的定义,大概意思就是每个组件占据父控件的空间的权重,如果不设置该属性,那么所有view的默认权重都为0.

下面通过举例说明该属性的作用
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
      <EditText android:id="@+id/edit_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message" />
      <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send" />
</LinearLayout>

效果:
这里写图片描述
如果用户输入的内容过长,那么输入框的长度会随之增加,这并不是我们想要的,但是我们又不想通过设置硬编码尺寸来限定这两个控件的长度,那么这时候layout-weight属性就派上用场了

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <EditText android:id="@+id/edit_message"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send" />
</LinearLayout>

To fill the remaining space in your layout with the EditText element, do the following:

  1. In the activity_my.xml file, assign the element’s
    layout_weight attribute a value of 1.

  2. Also, assign element’s layout_width attribute a value of 0dp.

To improve the layout efficiency when you specify the weight, you should change the width of the EditText to be zero (0dp). Setting the width to zero improves layout performance because using “wrap_content” as the width requires the system to calculate a width that is ultimately irrelevant because the weight value requires another width calculation to fill the remaining space.
Figure shows the result when you assign all weight to the EditText element.

<EditText
    android:layout_weight="1"
    android:layout_width="0dp"
    ... />

这里写图片描述
The EditText widget is given all the layout weight, so it fills the remaining space in the LinearLayout.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值