How do I prevent an EditText from resizing itself when the user is typing?

前端 未结 10 1975
粉色の甜心
粉色の甜心 2021-01-30 16:18

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

相关标签:
10条回答
  • 2021-01-30 16:46

    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());
    
    0 讨论(0)
  • 2021-01-30 16:48

    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"
    
    0 讨论(0)
  • 2021-01-30 16:50

    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.

    0 讨论(0)
  • 2021-01-30 16:51

    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.

    0 讨论(0)
  • 2021-01-30 16:52

    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.

    0 讨论(0)
  • 2021-01-30 16:52

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

    0 讨论(0)
提交回复
热议问题