根据 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>
...给了我这个:
我可以使用显式宽度值强制布局看起来或多或少,如下所示:
<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>
...看起来像这样:
...但是这样做(将特定的 dp val 分配给宽度)让我比一屋子摇椅中的猫更紧张。
基列有香脂吗?我的意思是,到底有没有一种分配百分比宽度的方法(当然,这确实有效)?
我使用 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>
...它没有:
它占据了宽度的一半而不是全部;我必须在 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>
...它看起来像这样:
IOW,在第一行,EditText 被一头古老的大象像苗条的训练师一样揉捏,在下一行,没有观察到尝试的宽度相等(虽然它实际上看起来不错,但这些标签并没有占用宽度的四分之一,但只有他们需要的,没有更多)。
而且,在所有情况下,当我将“.25”更改为“0.25”或“1”时,情况也是如此。
更新 3
好的,这就是我在 LinearLayout 中看到的,它的“layout_width”和“layout_height”属性具有“match_parent”和“wrap_content”的各种组合。
当两者都设置为 wrap_content 时:
当两者都设置为 match_parent 时:
如果 width 设置为 match_parent,height 设置为 wrap_content,则与 BOTH 设置为 match_parent 时相同。反之(height 设置为 match_parent,width 设置为 wrap_content)与设置为 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?)