diff options
author | Tor Norbye <tnorbye@google.com> | 2011-06-13 16:12:07 -0700 |
---|---|---|
committer | Tor Norbye <tnorbye@google.com> | 2011-06-13 16:25:18 -0700 |
commit | fdde9dd8b616e50f2db0fe9ec872718b143339c0 (patch) | |
tree | 23a7ae867ad9c870bb74716336f4030270064718 | |
parent | 1e9adf720237605516def4ef98922994fa74b01f (diff) | |
download | sdk-fdde9dd8b616e50f2db0fe9ec872718b143339c0.zip sdk-fdde9dd8b616e50f2db0fe9ec872718b143339c0.tar.gz sdk-fdde9dd8b616e50f2db0fe9ec872718b143339c0.tar.bz2 |
When distributing linear layout weights, also set size to 0dp. DO NOT MERGE
When you use the layout actions bar "Distribute Weights" in a
LinearLayout, in addition to setting all the weights to the same
nonzero value, also set the size (the height for a vertical linear
layout and the width for a horizontal one) to 0dp, to ensure that the
widgets are all given the same total size rather than sharing the
remaining space evenly.
In addition, when adding new widgets to a LinearLayout, see if all
elements in the linear layout already have nonzero and equal weights,
and if so duplicate this weight value on the new widget as well, and
similarly also duplicate 0dp/0dip/0px if used.
Change-Id: I78d1c5af05a8b8b54e4d1eb0d426ce592bc1fc69
2 files changed, 47 insertions, 0 deletions
diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/layout/LayoutConstants.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/layout/LayoutConstants.java index 9061702..0ef178d 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/layout/LayoutConstants.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/layout/LayoutConstants.java @@ -106,6 +106,7 @@ public class LayoutConstants { public static final String VALUE_TRUE = "true"; //$NON-NLS-1$ public static final String VALUE_FALSE= "false"; //$NON-NLS-1$ public static final String VALUE_N_DP = "%ddp"; //$NON-NLS-1$ + public static final String VALUE_ZERO_DP = "0dp"; //$NON-NLS-1$ // Gravity values. These have the GRAVITY_ prefix in front of value because we already // have VALUE_CENTER_HORIZONTAL defined for layouts, and its definition conflicts diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/layout/LinearLayoutRule.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/layout/LinearLayoutRule.java index ac4b6ff..47c5499 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/layout/LinearLayoutRule.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/layout/LinearLayoutRule.java @@ -26,6 +26,8 @@ import static com.android.ide.common.layout.LayoutConstants.ATTR_ORIENTATION; import static com.android.ide.common.layout.LayoutConstants.ATTR_WEIGHT_SUM; import static com.android.ide.common.layout.LayoutConstants.VALUE_HORIZONTAL; import static com.android.ide.common.layout.LayoutConstants.VALUE_VERTICAL; +import static com.android.ide.common.layout.LayoutConstants.VALUE_WRAP_CONTENT; +import static com.android.ide.common.layout.LayoutConstants.VALUE_ZERO_DP; import com.android.annotations.VisibleForTesting; import com.android.ide.common.api.DrawingStyle; @@ -220,8 +222,18 @@ public class LinearLayoutRule extends BaseLayoutRule { share = sum / numTargets; } String value = formatFloatAttribute((float) share); + String sizeAttribute = isVertical(parentNode) ? + ATTR_LAYOUT_HEIGHT : ATTR_LAYOUT_WIDTH; for (INode target : targets) { target.setAttribute(ANDROID_URI, ATTR_LAYOUT_WEIGHT, value); + // Also set the width/height to 0dp to ensure actual equal + // size (without this, only the remaining space is + // distributed) + if (VALUE_WRAP_CONTENT.equals( + target.getStringAttr(ANDROID_URI, sizeAttribute))) { + target.setAttribute(ANDROID_URI, + sizeAttribute, VALUE_ZERO_DP); + } } } else { assert action.getId().equals(ACTION_BASELINE); @@ -541,6 +553,40 @@ public class LinearLayoutRule extends BaseLayoutRule { node.setAttribute(ANDROID_URI, ATTR_LAYOUT_HEIGHT, fillParent); } } + + // If you insert into a layout that already is using layout weights, + // and all the layout weights are the same (nonzero) value, then use + // the same weight for this new layout as well. Also duplicate the 0dip/0px/0dp + // sizes, if used. + boolean duplicateWeight = true; + boolean duplicate0dip = true; + String sameWeight = null; + String sizeAttribute = isVertical(parent) ? ATTR_LAYOUT_HEIGHT : ATTR_LAYOUT_WIDTH; + for (INode target : parent.getChildren()) { + if (target == node) { + continue; + } + String weight = target.getStringAttr(ANDROID_URI, ATTR_LAYOUT_WEIGHT); + if (weight == null || weight.length() == 0) { + duplicateWeight = false; + break; + } else if (sameWeight != null && !sameWeight.equals(weight)) { + duplicateWeight = false; + } else { + sameWeight = weight; + } + String size = target.getStringAttr(ANDROID_URI, sizeAttribute); + if (size != null && !size.startsWith("0")) { //$NON-NLS-1$ + duplicate0dip = false; + break; + } + } + if (duplicateWeight && sameWeight != null) { + node.setAttribute(ANDROID_URI, ATTR_LAYOUT_WEIGHT, sameWeight); + if (duplicate0dip) { + node.setAttribute(ANDROID_URI, sizeAttribute, VALUE_ZERO_DP); + } + } } /** A possible match position */ |