EditText y Button en la misma línea con TextView debajo?

I am trying to have an EditText widget next to a Button, also with a TextView debajo. La EditText should resize and fill the screen accordingly, while the Button's width should always be only what it needs to be (I did this by setting the width to wrap_content).

The layout I am trying to accomplish should no es be relative. Below is the code I have so far (some of which was found here on StackOverflow). Removing the TextView gives the desired appearance for the EditText and Button, but when the TextView is added, the view goes pretty awry.

Any insight would help!

<?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_height="wrap_content" 
                android:orientation="horizontal"
                android:layout_width="fill_parent"
  >
    <EditText android:text="@+id/EditText01" 
              android:id="@+id/EditText01"
              android:layout_height="wrap_content" 
              android:layout_weight="1"
              android:layout_width="fill_parent"
    />

    <Button android:text="@+id/Button01" 
            android:id="@+id/Button01"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
    />

    <TextView android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="@string/hello"
    />

</LinearLayout>

preguntado el 09 de enero de 11 a las 02:01

The layout should not be relative. ¿Por qué? -

Stack overflow doesn't like the XML tags. It thinks it's HTML and strips it out. Instead of the <code><pre> tags, highlight the code and click the code button to format it -

Relative will most likely needlessly complicate things in the future. I am new to android programming but not to programming in general. Having to specify positions all the time ends up very painful in the end. If you can do it via relative layout I would still like to see how. Also thank you for letting me know about the code formatting here on StackOverflow. -

1 Respuestas

Prueba esto:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" android:orientation="horizontal"
    android:layout_width="fill_parent">

    <Button

        android:text="@+id/Button01" 
        android:id="@+id/Button01"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true">
    </Button>

    <EditText android:text="@+id/EditText01" 
        android:id="@+id/EditText01"
        android:layout_height="wrap_content" 
        android:layout_toLeftOf="@id/Button01"
        android:layout_width="fill_parent">
    </EditText>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/EditText01"
        android:text="@string/hello"
    />
</RelativeLayout>

Respondido el 09 de enero de 11 a las 06:01

Worked perfectly Falmarri. Adding <code>android:padding="5dip"</code> also helped make the look more uniform when compared to the LinearLayout. - Kevin Parker

While we're at it... Now how would one add additional TextView(s) just below the first? - Kevin Parker

Do the same thing. Give the TextView an id like you did the button and edittext, and on your new TextVIew set its layout_below="@id/TextView02 or whatever the id is. - Falmarri

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.