Как мне динамически увеличить размер EditText в Android?

#android #android-studio #android-layout #android-edittext

#Android #android-studio #android-layout #android-edittext

Вопрос:

Я хочу, чтобы высота EditText динамически увеличивалась в соответствии с размером вводимого текста.

Точно так же, как Whatsapp.

Вот что я сейчас делаю!

  <LinearLayout
    android:layout_alignParentBottom="true"
    android:layout_width="wrap_content"
    android:layout_height="44dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginBottom="8dp"
    android:orientation="horizontal">


    <EditText
        android:textSize="15dp"
        android:textColor="#000000"
        android:hint="Enter New Note"
        android:textColorHint="#8B111111"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_gravity="center_vertical"
        android:gravity="center"
        android:imeOptions="actionDone"
        android:layout_marginTop="8dp"
        android:contentDescription="Enter Input Here"
        android:layout_marginBottom="8dp"
        android:id="@ id/editextNew"/>

</LinearLayout>
  

Комментарии:

1. просто войдите в acitvity и укажите ширину / высоту этого 🙂

2. Извините, я не понял @ParthPitroda

3. измените высоту, чтобы обернуть содержимое, и добавьте максимальное количество строк к желаемому количеству строк.

4. после этого размер EditText становится очень маленьким при запуске! @RituSumanMohanty

5. Ваш LinearLayout по-прежнему имеет высоту 44dp.

Ответ №1:

Пожалуйста, попробуйте это :

  <LinearLayout
    android:layout_alignParentBottom="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginBottom="8dp"
    android:orientation="horizontal">


    <EditText
        android:textSize="15dp"
        android:textColor="#000000"
        android:hint="Enter New Note"
        android:maxLines="40"
        android:lines="20"
        android:textColorHint="#8B111111"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:gravity="center"
        android:imeOptions="actionDone"
        android:layout_marginTop="8dp"
        android:contentDescription="Enter Input Here"
        android:layout_marginBottom="8dp"
        android:id="@ id/editextNew"/>

</LinearLayout>
  

Ответ №2:

Единственное изменение, которое вам нужно внести (оба варианта): android:layout_height="wrap_content"

Ответ №3:

Потому что вы присваиваете ему значение fixed_height. Измените это на wrap_content, и это должно сработать.

Вы можете добавить android: maxLines, чтобы ограничить его расширение.