How do I prevent an EditText from resizing itself

2019-03-08 07:16发布

I have an EditText and a Button set next to each other on the same horizontal line. It looks great, except when the user enters a lot of text, the EditText is resized, and the Button is squished.

I have both EditText and Button set to layout_width="wrap_content". "fill_parent" messes up the layout, and I don't want to use absolute sizes if I don't have to - the way it is now looks great in both landscape and portrait, I just don't want the EditText to resize.

My 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>

10条回答
等我变得足够好
2楼-- · 2019-03-08 07:37

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"
查看更多
祖国的老花朵
3楼-- · 2019-03-08 07:40

Give EditText a maxWidth. That should stop it from resizing beyond the width you provided. Also, if you want it to be contained within 1 line set the maxLines setting to 1 too.

查看更多
爷、活的狠高调
4楼-- · 2019-03-08 07:42

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!

查看更多
霸刀☆藐视天下
5楼-- · 2019-03-08 07:47

enter image description here

You need to add android:imeOptions to prevent this behavior.

Click here

查看更多
成全新的幸福
6楼-- · 2019-03-08 07:47

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"
查看更多
我只想做你的唯一
7楼-- · 2019-03-08 07:48

What I do is in the onCreate for the activity, measure the EditText first then apply its maxWidth.

You can do so using code similar to the following:

EditText someedittext = (EditText)findViewById(R.id.sometextview);
someedittext.setMaxWidth(someedittext.getWidth());
查看更多
登录 后发表回答