问题
Based on how XAML works, I thought I could give my android widgets a percentage value for width. After searching, I found that *theoretically," this is available via the "layout_weight" property. But using this does not produce the desired appearance. Specifically, this 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>
...gives me this:
I can brute-force the layout to look more-or-less as I want it using explicit width values, like so:
<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>
...which looks like this:
...but doing this (assigning specific dp vals to the width) makes me more nervous than a cat in a roomful of rocking chairs.
Is there balsam in Gilead? I mean, is there, after all, a way to assign percentage widths (that actually works, of course)?
Am I using layout_weight wrong, or am I using the wrong methodology, or is it (perish the thought) impossible?
UPDATE
The accepted answer worked in one instance, but in the next:
<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>
...it doesn't:
It takes up half the width instead of all of it; must I put some "replace" talk in the EditText widgets for them to "widen out"?
I can "force it" by adding to the EditTexts:
android:minWidth="120dp"
...but that returns me to the nervosity level of the rocking-chair-room cat.
UPDATE 2
Now even what I thought was working is not, or no longer. The xml is this:
<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>
...and it looks like this:
IOW, in the first row, the EditText is getting scrunched like a svelte trainer by an age-old elephant, and in the next row, the attempted equality of widths is not being observed (although it actually looks fine, those labels are not taking one-quarter of the width, but only what they need and no more).
And, the same is the case when I change ".25" to either "0.25" or "1" in all cases.
UPDATE 3
Okay, here is what I see with my LinearLayout with various combinations of "match_parent" and "wrap_content" for its "layout_width" and "layout_height" properties.
When both are set to wrap_content:
When both are set to match_parent:
If width is set to match_parent, and height is set to wrap_content, it is the same as when BOTH are set to match_parent The other way around (with height set to match_parent and width set to wrap_content), it is the same as when BOTH are set to wrap_content
UPDATE 4
Here is the entire contents of this particular layout file, at its best, albeit not perfect by any means:
<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>
UPDATE 5
Okay, what I'm getting out of this is that the internal-most LinearLayouts need to be thus:
android:layout_width="match_parent"
android:layout_height="wrap_content"
...and all others (above them) thus:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
That works for the most part. But the EditTexts are too puny. If I remove the inner LinearLayouts completely, the EditTexts disappear (or have dimensions of 0?)
回答1:
You should have a single LinearLayout containing all the "weighted" widgets.
Note that only 1 dimension at a time can be "weighted":
This is how I'd do that:
<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>
Note that the weight sum is 1 (android calculates it by itself).
I could set 4 and 6 or 40 and 60 - for Android that's always 100%, when summed.
You can optionally set a weightSum attribute in the Containing LinearLayout:
android:weightSum="1"
(or 10, or 100, ...)
If you want to (and your layout isn't complex), you can weight the other "dimension" (height in this case), too.
Just add another LinearLayout containing another Spinner and EditText.
Then enclose these 2 LinearLayouts in a third one.
Give both the children LinearLayouts a weight of 1 and a height of 0dp, so they will equally divide the container's height.
回答2:
Some thing like that:
<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>
or
<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>
来源:https://stackoverflow.com/questions/23894675/how-can-i-give-my-widgets-width-a-percentage-rather-than-an-explicit-value