android:layout_weight是什么意思?

我不明白如何使用这个属性。 谁能告诉我更多关于它的事情? android


#1楼

添加到其余答案,使这个工做最重要的是将布局宽度(或高度)设置为0px 布局

android:layout_width="0px"

不然你会看到垃圾 性能


#2楼

结合两个答案 spa

Flo&rptwsthi和roetzi, code

请记住更改layout_width=0dp/px ,不然layout_weight行为将反向行动,最大数量占用最小空间,最小数量占用最大空间。 xml

此外,一些权重组合会致使一些布局没法显示(由于它占据了空间)。 utf-8

要当心这一点。 get


#3楼

顾名思义,布局权重指定特定字段或窗口小部件占据屏幕空间的空间量或百分比。
若是咱们在水平方向指定权重,那么咱们必须指定layout_width = 0px
一样,若是咱们在垂直方向指定权重,那么咱们必须指定layout_height = 0px数学


#4楼

layout_weight告诉Android如何在LinearLayout分发您的View 。 Android而后首先计算全部具备指定权重的View所需的总比例,并根据所需的屏幕分数放置每一个View 。 在下面的示例中,Android看到TextViewlayout_weight0 (这是默认值), EditTextlayout_weight2 ,而Button的权重为1 。 所以Android分配“刚够”的空间来显示tvUsernametvPassword而后将所述屏幕宽度的剩余部分分红5个等份,其中的两个被分配给etUsername ,两到etPassword和最后部分bLoginit

<LinearLayout android:orientation="horizontal" ...>

    <TextView android:id="@+id/tvUsername" 
    android:text="Username" 
    android:layout_width="wrap_content" ... />

    <EditText android:id="@+id/etUsername"
    android:layout_width="0dp"
    android:layout_weight="2" ... />

    <TextView android:id="@+id/tvPassword"
    android:text="Password"
    android:layout_width="wrap_content" />

    <EditText android:id="@+id/etPassword"
    android:layout_width="0dp"
    android:layout_weight="2" ... />

    <Button android:id="@+id/bLogin"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:text="Login"... />

</LinearLayout>

看起来像:
景观取向
纵向


#5楼

若是有多个视图跨越LinearLayout ,则layout_weight会为每一个视图提供一个比例大小。 具备更大layout_weight值的视图“更重”,所以它得到更大的空间。

这是一个让事情更清晰的图像。

在此输入图像描述

理论

术语布局权重与数学中加权平均的概念有关。 就像在大学课堂上,做业价值30%,出勤率10%,期中价值20%,决赛价值40%。 这些部分的分数在加权后会给出总分。

在此输入图像描述

布局重量也是同样的。 水平LinearLayoutViews能够占据总宽度的必定百分比。 (或垂直LinearLayout的高度的百分比。)

布局

您使用的LinearLayout将以下所示:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <!-- list of subviews -->

</LinearLayout>

请注意,必须对LinearLayout使用layout_width="match_parent" 。 若是你使用wrap_content ,那么它将没法工做。 另请注意, layout_weight不适用于RelativeLayouts中的视图(有关此问题的SO答案,请参阅此处此处 )。

观点

水平LinearLayout每一个视图都以下所示:

<Button
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1" />

请注意,您须要将layout_width="0dp"layout_weight="1"一块儿使用。 忘记这会致使许多新用户出现问题。 (请参阅此文章 ,了解经过不将宽度设置为0可得到的不一样结果。)若是您的视图位于垂直 LinearLayout那么您固然会使用layout_height="0dp"

在上面的Button示例中,我将权重设置为1,但您可使用任何数字。 重要的只是总数。 您能够在我发布的第一张图像中的三行按钮中看到,数字都不一样,但因为比例相同,所以每行的加权宽度不会改变。 有些人喜欢使用总和为1的十进制数,这样在复杂的布局中,每一个部分的重量都很清楚。

最后一点说明。 若是您有许多使用layout_weight的嵌套布局,则可能对性能不利。

额外

如下是顶部图像的xml布局:

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android:layout_weight="
        android:textSize="24sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="2" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />

    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android:layout_weight="
        android:textSize="24sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="10"
            android:text="10" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="20"
            android:text="20" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="10"
            android:text="10" />

    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android:layout_weight="
        android:textSize="24sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:text=".25" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".50"
            android:text=".50" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:text=".25" />

    </LinearLayout>

</LinearLayout>
相关文章
相关标签/搜索