OSDN Git Service

Make edit texts grow in horizontal LinearLayouts using weights
authorTor Norbye <tnorbye@google.com>
Fri, 18 Mar 2011 03:53:52 +0000 (20:53 -0700)
committerTor Norbye <tnorbye@google.com>
Fri, 18 Mar 2011 18:33:36 +0000 (11:33 -0700)
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

eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/common/layout/LinearLayoutRule.java
eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/newxmlfile/NewXmlFileCreationPage.java
eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/layout/refactoring/testdata/quickfix1-expected-quickFix3.xml

index 1e2d0e3..4b9d006 100644 (file)
@@ -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);
index f34c4b8..a797bd0 100644 (file)
@@ -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