Wie verhindern Sie, dass ein EditText die Größe selbst beim Eingeben des Benutzers angreift?

StackOverflow https://stackoverflow.com/questions/1408781

Frage

ich habe ein EditText und ein Button Stellen Sie auf derselben horizontalen Linie nebeneinander ein. Es sieht gut aus, außer wenn der Benutzer viel Text eingibt, die EditText wird geändert und die Button ist gequetscht.

ich habe beides EditText und Button einstellen layout_width="wrap_content". "fill_parent" Das Layout durcheinander bringt, und ich möchte keine absoluten Größen verwenden, wenn ich nicht muss - so wie es jetzt in Landschaft und Porträt großartig aussieht. Ich will das einfach nicht EditText Größe ändern.

Mein Layout:

<TableLayout
    android:id="@+id/homelayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TableRow>
        <TextView
            android:id="@+id/labelartist"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Find artists:" />
    </TableRow>
    <TableRow>
        <EditText
            android:id="@+id/entryartist"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="6"
            android:background="@android:drawable/editbox_background"
            android:editable="true"
            android:padding="5px"
            android:singleLine="true" />
        <Button
            android:id="@+id/okartist"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginLeft="10dip"
            android:layout_weight="1"
            android:text="Search" />
    </TableRow>

</TableLayout>
War es hilfreich?

Lösung

Für die Verwendung von Bearbeiten

android:layout_width="0dp" - not required but preferred.
android:layout_weight="1"

Und für die Schaltfläche nicht angeben android:layout_weight

Andere Tipps

Was ich für die Aktivität in der OnCreate tue, messen Sie zuerst den EditText und wenden Sie dann seine Maxwidth an.

Sie können dies mit Code mit ähnlicher Weise wie folgt tun:

EditText someedittext = (EditText)findViewById(R.id.sometextview);
someedittext.setMaxWidth(someedittext.getWidth());

Geben EditText a maxWidth. Dies sollte verhindern, dass die Größe über die Breite hinausgeht, die Sie zur Verfügung gestellt haben. Auch wenn Sie möchten, dass es innerhalb von 1 Zeile enthalten ist maxLines Einstellung auf 1 auch.

Benutz einfach:

android:layout_width="fill_parent"

sowohl für editText als auch für die Schaltfläche. Es funktioniert auch für EditText, aber es ist besser, es auf beiden zu haben.

enter image description here

Sie müssen Android: ImeOptions hinzufügen, um dieses Verhalten zu verhindern.

Klick hier

you can define size of your edittext as it shows above

android:minWidth="80dp"
android:maxWidth="80dp"

or

android:layout_width="60dp"

or

you can apply a background image of size you want but don't forget to define its height and width with wrap_content.

  android:layout_width="wrap_content"
  android:layout_height="wrap_content"

I had a similar problem, but I couldn't get the above to work. What I did, is take in the text from EditText box and then format it down to the max size that my screen could handle. When I save this off to the DB, I will save it as display Text and original text.

If you go to this page I put a longer explanation.

The correct way to do this would be to set the android:minWidth and android:maxWidth equal to the width you want. This way you will not have to adjust your layout to use the android:layout_weight.

For example if you want your EditText to always be 80 density pixels no matter how many characters you type in use the following:

android:minWidth="80dp"
android:maxWidth="80dp"

I had the exact query. But i had an EditText and a Button next to each other in a Linear Layout. I tried android:layout_weight="1" for EditText android:layout_width="0dp" And it worked just perfect!! Thanks a lot!

The chosen solution works, but let me add a little complement:
if you use android:layout_weight="0.15" (the value is not important)
then
android:layout_width="0dp" if the LinearLayout is Horizontal
or
android:layout_height="0dp" if the LinearLayout is Vertical.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top