From c6a4984b10644bc0cb3a02bdda423b2a836234f0 Mon Sep 17 00:00:00 2001 From: Tor Norbye Date: Thu, 17 Mar 2011 20:53:52 -0700 Subject: [PATCH] Make edit texts grow in horizontal LinearLayouts using weights If you drop a text field in a vertical linear layout, then the *width* of the text field is automatically set to fill. This uses metadata to check what the fill preference is of each dropped view. This changeset extends this scheme to also grow text fields in a horizontal linear layout. However, instead of using fill_parent on the layout_width, this uses a layout_weight instead. This means that the text field will fill, but it will not push subsequent children out of the way the way a fill_parent would. This changeset also changes the default orientation of a LinearLayout that is created in new XML files. (I looked around and noticed most layouts that have a LinearLayout at the root will use a vertical LinearLayout.) Change-Id: I52eec4841e4bac076da655c603235547cfd46b73 --- .../com/android/ide/common/layout/LinearLayoutRule.java | 5 +++++ .../wizards/newxmlfile/NewXmlFileCreationPage.java | 14 ++++++++++---- .../refactoring/testdata/quickfix1-expected-quickFix3.xml | 1 + 3 files changed, 16 insertions(+), 4 deletions(-) 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 1e2d0e320..4b9d0062c 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 @@ -530,6 +530,11 @@ public class LinearLayoutRule extends BaseLayoutRule { String fillParent = getFillParentValueName(); if (fill.fillHorizontally(vertical)) { node.setAttribute(ANDROID_URI, ATTR_LAYOUT_WIDTH, fillParent); + } else if (!vertical && fill == FillPreference.WIDTH_IN_VERTICAL) { + // In a horizontal layout, make views that would fill horizontally in a + // vertical layout have a non-zero weight instead. This will make the item + // fill but only enough to allow other views to be shown as well. + node.setAttribute(ANDROID_URI, ATTR_LAYOUT_WEIGHT, "1"); //$NON-NLS-1$ } if (fill.fillVertically(vertical)) { node.setAttribute(ANDROID_URI, ATTR_LAYOUT_HEIGHT, fillParent); diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/newxmlfile/NewXmlFileCreationPage.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/newxmlfile/NewXmlFileCreationPage.java index f34c4b879..a797bd09a 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/newxmlfile/NewXmlFileCreationPage.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/newxmlfile/NewXmlFileCreationPage.java @@ -17,6 +17,9 @@ package com.android.ide.eclipse.adt.internal.wizards.newxmlfile; +import static com.android.ide.common.layout.LayoutConstants.VALUE_FILL_PARENT; +import static com.android.ide.common.layout.LayoutConstants.VALUE_MATCH_PARENT; + import com.android.AndroidConstants; import com.android.ide.common.resources.configuration.FolderConfiguration; import com.android.ide.common.resources.configuration.ResourceQualifier; @@ -234,17 +237,20 @@ class NewXmlFileCreationPage extends WizardPage { @Override String getDefaultAttrs(IProject project) { Sdk currentSdk = Sdk.getCurrent(); + String fill = VALUE_FILL_PARENT; if (currentSdk != null) { IAndroidTarget target = currentSdk.getTarget(project); // fill_parent was renamed match_parent in API level 8 if (target != null && target.getVersion().getApiLevel() >= 8) { - return "android:layout_width=\"match_parent\"\n" //$NON-NLS-1$ - + "android:layout_height=\"match_parent\""; //$NON-NLS-1$ + fill = VALUE_MATCH_PARENT; } } - return "android:layout_width=\"fill_parent\"\n" //$NON-NLS-1$ - + "android:layout_height=\"fill_parent\""; //$NON-NLS-1$ + return String.format( + "android:orientation=\"vertical\"\n" //$NON-NLS-1$ + + "android:layout_width=\"%1$s\"\n" //$NON-NLS-1$ + + "android:layout_height=\"%1$s\"", //$NON-NLS-1$ + fill, fill); } }, new TypeInfo("Values", // UI name diff --git a/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/testdata/quickfix1-expected-quickFix3.xml b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/testdata/quickfix1-expected-quickFix3.xml index 14256f4b3..c8333cb23 100644 --- a/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/testdata/quickfix1-expected-quickFix3.xml +++ b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/testdata/quickfix1-expected-quickFix3.xml @@ -1,6 +1,7 @@ ^ -- 2.11.0