我怎样才能给我的小部件'宽度百分比而不是显式值?

时间:2014-05-27 16:40:20

标签: android android-layout layout android-linearlayout android-layout-weight

根据XAML的工作原理,我想我可以给我的android小部件一个宽度的百分比值。在搜索之后,我发现*理论上,“这可以通过”layout_weight“属性获得。但是使用它不会产生所需的外观。具体来说,这个xml:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dp">

    <Spinner
        android:id="@+id/spinnerUPCPLU"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".40"
        android:entries="@array/delivery_upcplu_spinner" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/greyframe"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/editTextUPCPLU"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".90"
            android:editable="false" />
    </LinearLayout>

</LinearLayout>

...给我这个:

enter image description here

我可以使用显式宽度值来强制布局看起来或多或少,如下所示:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dp">

    <Spinner
        android:id="@+id/spinnerUPCPLU"
        android:layout_width="160dp"
        android:layout_height="40dp"
        android:entries="@array/delivery_upcplu_spinner" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/greyframe"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/editTextUPCPLU"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:editable="false"
            android:minWidth="200dp" />
    </LinearLayout>

</LinearLayout>

......看起来像这样:

enter image description here

...但是这样做(给宽度分配特定的dp值)让我比一把摇椅上的猫更紧张。

基列有苦瓜吗?我的意思是,毕竟,是否有一种分配百分比宽度的方法(实际上当然有效)?

我使用layout_weight是错误的,还是我使用了错误的方法,或者它是否(使思想消失)不可能?

更新

接受的答案在一个例子中起作用,但在下一个例子中:

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:text="@string/id"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editTextID"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".25" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:text="@string/pack_size"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editTextPackSize"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".25" />
    </LinearLayout>

......它没有:

enter image description here

它占据了宽度的一半而不是全部宽度;我必须在EditText小部件中添加一些“替换”对话,以便“扩大”吗?

我可以通过添加到EditTexts来强制它:

android:minWidth="120dp"

......但是这让我回到了摇椅室猫的神经紧张程度。

更新2

现在,即使我认为工作的也不是,或者不再。 xml是这样的:

<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="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="4dp"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="hhs.app.DeliveryActivity">

    <!--Row 0-->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="5dp"
        >
        <Spinner
            android:id="@+id/spinnerUPCPLU"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".10"
            android:maxWidth="12dp"
            android:entries="@array/delivery_upcplu_spinner"
            />
        <EditText
            android:id="@+id/editTextUPCPLU"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".90"
            android:minWidth="1200dp"
            android:editable="false"
            />
    </LinearLayout>

    <!--Row 1-->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="5dp">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:text="@string/id"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/greyframe"
            android:orientation="horizontal">
        <EditText
            android:id="@+id/editTextID"
            android:layout_width="0dp"
            android:minWidth="120dp"
            android:layout_height="wrap_content"
            android:layout_weight=".25" />
        </LinearLayout>

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:text="@string/pack_size"
            android:textAppearance="?android:attr/textAppearanceMedium" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/greyframe"
                android:orientation="horizontal">
        <EditText
            android:id="@+id/editTextPackSize"
            android:layout_width="0dp"
            android:minWidth="120dp"
            android:layout_height="wrap_content"
            android:layout_weight=".25" />
            </LinearLayout>
    </LinearLayout>

......它看起来像这样:

enter image description here

IOW,在第一行中,EditText被一个古老的大象像一个苗条的教练一样被碾碎,在下一行中,没有观察到尝试的宽度相等(虽然它实际上看起来很好,那些标签是占宽度的四分之一,但仅限于他们需要的而不是更多。)

而且,在所有情况下我将“.25”改为“0.25”或“1”的情况也一样。

更新3

好的,这就是我在LinearLayout中看到的“match_parent”和“wrap_content”各种组合的“layout_width”和“layout_height”属性。

当两者都设置为wrap_content时:

enter image description here

当两者都设置为match_parent时:

enter image description here

如果width设置为match_parent,并且height设置为wrap_content,则与将BOTH设置为match_parent时相同 反过来(高度设置为match_parent,宽度设置为wrap_content),它与将BOTH设置为wrap_content时相同

更新4

以下是此特定布局文件的全部内容,尽管不是完美无缺:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <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="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="4dp"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="hhs.app.DeliveryActivity">

        <!--Row 0-->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:padding="5dp"
            >
            <Spinner
                android:id="@+id/spinnerUPCPLU"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".10"
                android:maxWidth="12dp"
                android:entries="@array/delivery_upcplu_spinner"
                />
            <EditText
                android:id="@+id/editTextUPCPLU"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight=".90"
                android:minWidth="1200dp"
                android:editable="false"
                />
        </LinearLayout>

        <!--Row 1-->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="5dp">

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="25"
                android:text="@string/id"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/greyframe"
                android:orientation="horizontal">
            <EditText
                android:id="@+id/editTextID"
                android:layout_width="0dp"
                android:minWidth="120dp"
                android:layout_height="wrap_content"
                android:layout_weight="25" />
            </LinearLayout>

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="25"
                android:text="@string/pack_size"
                android:textAppearance="?android:attr/textAppearanceMedium" />

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/greyframe"
                    android:orientation="horizontal">
            <EditText
                android:id="@+id/editTextPackSize"
                android:layout_width="0dp"
                android:minWidth="120dp"
                android:layout_height="wrap_content"
                android:layout_weight="25" />
                </LinearLayout>
        </LinearLayout>

        <!--Row 2-->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="5dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/desc"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/greyframe"
                android:orientation="horizontal">

                <EditText
                    android:id="@+id/editTextDesc"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:minWidth="320dp" />
            </LinearLayout>

        </LinearLayout>

        <!--Row 3-->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="5dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/qty"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/orangeframe"
                android:orientation="horizontal">

                <EditText
                    android:id="@+id/editTextQty"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:minWidth="144dp" />
            </LinearLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/count"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/greyframe"
                android:orientation="horizontal">

                <EditText
                    android:id="@+id/editTextCount"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:editable="false"
                    android:minWidth="144dp" />
            </LinearLayout>

        </LinearLayout>

        <!--Row 4-->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="5dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/cost"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/orangeframe"
                android:orientation="horizontal">

                <EditText
                    android:id="@+id/editCost"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:minWidth="48dp" />
            </LinearLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/margin"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/orangeframe"
                android:orientation="horizontal">

                <EditText
                    android:id="@+id/editTextMargin"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:minWidth="48dp" />
            </LinearLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/list"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/orangeframe"
                android:orientation="horizontal">

                <EditText
                    android:id="@+id/editList"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:minWidth="48dp" />
            </LinearLayout>

        </LinearLayout>

        <!--Row 5-->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="5dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/dept"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/greyframe"
                android:orientation="horizontal">

                <Spinner
                    android:id="@+id/spinnerDept"
                    android:layout_width="180dp"
                    android:layout_height="40dp"
                    android:entries="@array/departments" />
            </LinearLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="$"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/greyframe"
                android:orientation="horizontal">

                <EditText
                    android:id="@+id/editTextDollar"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:minWidth="48dp" />
            </LinearLayout>

        </LinearLayout>

        <!--Row 6-->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="5dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/sub_dept"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/greyframe"
                android:orientation="horizontal">

                <Spinner
                    android:id="@+id/spinnerSubdept"
                    android:layout_width="248dp"
                    android:layout_height="40dp"
                    android:entries="@array/subdepartments" />
            </LinearLayout>

        </LinearLayout>

        <!--Row 7-->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/box"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="5dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/delivery_invoice_number"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/greyframe"
                    android:orientation="horizontal">

                    <EditText
                        android:id="@+id/editTextDelivInvNum"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:editable="false"
                        android:minWidth="124dp" />
                </LinearLayout>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/vendor"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/greyframe"
                    android:orientation="horizontal">

                    <EditText
                        android:id="@+id/editTextVendor"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:editable="false"
                        android:minWidth="124dp" />
                </LinearLayout>

            </LinearLayout>

            <!--Row 8-->
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="5dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/total_dollars"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/greyframe"
                    android:orientation="horizontal">

                    <EditText
                        android:id="@+id/editTextTotalDollars"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:minWidth="80dp" />
                </LinearLayout>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/current_total"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/greyframe"
                    android:orientation="horizontal">

                    <EditText
                        android:id="@+id/editTextCurrentTotal"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:minWidth="80dp" />
                </LinearLayout>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/qty"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/greyframe"
                    android:orientation="horizontal">

                    <EditText
                        android:id="@+id/editTextReadonlyQty"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:minWidth="40dp" />
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>

        <!--Row 9-->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="5dp">

            <Button
                android:id="@+id/buttonSave"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/save" />

            <Button
                android:id="@+id/buttonFind"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/find" />

            <Button
                android:id="@+id/buttonClear"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/clear" />
        </LinearLayout>

    </LinearLayout>
</ScrollView>

更新5

好的,我从中得到的是内部最多的LinearLayouts因此需要:

android:layout_width="match_parent"
android:layout_height="wrap_content"

......以及所有其他人(在他们之上)因此:

android:layout_width="wrap_content"
android:layout_height="wrap_content"

大部分都有效。但EditTexts太过于微不足道了。如果我完全删除内部LinearLayouts,EditTexts会消失(或者尺寸为0?)

2 个答案:

答案 0 :(得分:3)

你应该有一个包含所有“加权”小部件的LinearLayout 请注意,一次只能“加权”一维:

我就是这样做的:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="8dp"
    >
    <Spinner
        android:id="@+id/spinnerUPCPLU"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".40"
        android:entries="@array/delivery_upcplu_spinner"
    />
    <EditText
        android:id="@+id/editTextUPCPLU"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".60"
        android:editable="false"
    />
</LinearLayout>

请注意,权重总和为1(android自行计算) 我可以设置4和6或40和60 - 对于Android来说总和是100%,总结时。

您可以选择在包含LinearLayout中设置weightSum属性:

android:weightSum="1"

(或10或100,......)

如果你想(并且你的布局不复杂),你也可以加权另一个“尺寸”(在这种情况下是高度)。

只需添加另一个包含另一个Spinner和EditText的LinearLayout。

然后将这2个LinearLayouts包含在第三个中。

让两个孩子的LinearLayouts重量为 1 ,高度为 0dp ,因此他们将同等划分容器的高度。

答案 1 :(得分:1)

有些事情是这样的:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="8dp"
    >
    <Spinner
        android:id="@+id/spinnerUPCPLU"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="50"
        android:entries="@array/delivery_upcplu_spinner"
    />
    <EditText
        android:id="@+id/editTextUPCPLU"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="50"
        android:editable="false"
    />
</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="8dp"
    >
    <Spinner
        android:id="@+id/spinnerUPCPLU"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="70"
        android:entries="@array/delivery_upcplu_spinner"
    />
    <EditText
        android:id="@+id/editTextUPCPLU"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="30"
        android:editable="false"
    />
</LinearLayout>