How to make Android EditText expand vertically whe

2020-06-10 06:48发布

I have this EditText

<EditText
    android:layout_width="match_parent"
    android:layout_height="72dp"
    android:hint="@string/write_message"
    android:textColorHint="@color/primary_color"
    android:ems="10"
    android:imeOptions="actionDone"
    android:inputType="textImeMultiLine"
    android:id="@+id/message_input"
    android:layout_gravity="center_horizontal"
    android:backgroundTint="@color/primary_color"/>

When the box is filled up with user inputted text, it scrolls to the right to make room for more text, I do not like this behavior, and would prefer it if the EditText box expanded upwards when it needs more room? Is there a way to do this? Thanks.

1条回答
我命由我不由天
2楼-- · 2020-06-10 07:33

Yes, this actually involves two things:

  1. Making the EditText accept multi-line input.
  2. Having its height grow as more text lines are added.

Therefore, to achieve this, you need to set up:

android:inputType="textMultiLine"
android:layout_height="wrap_content"

(Be mindful of the difference between textMultiLine and textImeMultiLine).

The full XML snippet would be:

<EditText
    android:layout_width="match_parent"
    android:inputType="textMultiLine"
    android:layout_height="wrap_content"
    android:hint="@string/write_message"
    android:textColorHint="@color/primary_color"
    android:ems="10"
    android:imeOptions="actionDone"
    android:id="@+id/message_input"
    android:layout_gravity="center_horizontal"
    android:backgroundTint="@color/primary_color"/>    
查看更多
登录 后发表回答