根据 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 值)让我比满屋子的摇椅上的猫还紧张。
吉利德有香脂吗?我的意思是,到底有没有办法分配百分比宽度(当然,这确实有效)?
我使用的 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 像一头古老的大象的 slim 教练一样被挤压,而在下一行中,没有观察到尝试的宽度相等(尽管它实际上看起来不错,那些标签不占宽度的四分之一,但只占他们需要的宽度。
而且,当我在所有情况下将“.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时一样 反过来(高度设置为 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?)
最佳答案
您应该有一个包含所有“加权”小部件的 LinearLayout。
请注意,一次只能对 1 个维度进行“加权”:
我会这样做:
<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%。
您可以有选择地在 Containing LinearLayout 中设置一个 weightSum 属性:
android:weightSum="1"
(或 10,或 100,...)
如果您愿意(并且您的布局并不复杂),您也可以加权其他“维度”(在本例中为高度)。
只需添加另一个包含另一个 Spinner 和 EditText 的 LinearLayout。
然后将这 2 个 LinearLayouts 包含在第三个中。
为子 LinearLayouts 赋予 1 的权重和 0dp 的高度,因此它们将平均划分容器的高度。
关于android - 我怎样才能给小部件的宽度一个百分比而不是一个明确的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23894675/