EditText растет и выталкивает другой вид за пределы экрана - PullRequest
0 голосов
/ 05 апреля 2019

У меня есть макет с двумя представлениями, который должен вести себя так:

  • Сверху находится текст редактирования, который должен динамически расти (и уменьшаться). Прямо под ним другой взгляд. EditText растет, пока другой Вид достигает нижней части родительского макета, затем перестает расти и становится вместо этого прокручиваемым.

Изображение, иллюстрирующее желаемое поведение, можно найти здесь (не может загрузить его):

enter image description here

Проблема, похоже, заключается в том, чтобы ограничить EditText определенной высотой, чтобы он не оттолкнул другой вид от нижней части экрана. Или дать другому View атрибут, который всегда остается на экране, я не знаю.

Я пробовал несколько разных подходов, чтобы решить эту проблему с помощью XML. Тот, который я прикрепляю, просто работает, ограничивая количество строк в EditText - которое мне позже придется менять программно в зависимости от разрешения устройства. Так что это не очень желательно.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"   
    android:orientation="vertical">

    <include
        android:id="@+id/tripNotes_toolbar_layout"
        layout="@layout/toolbar_details" />

    <android.support.constraint.ConstraintLayout
        android:id="@+id/tripNotes_constraintLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        style="@style/CardStyle"

        android:layout_gravity="top"
        android:layout_marginLeft="@dimen/card_outer_margin_left"
        android:layout_marginRight="@dimen/card_outer_margin_left">

        <EditText
            android:id="@+id/tripNotes_editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:layout_margin="8dp"
            android:background="@null"
            android:hint="Put your notes here"
            android:inputType="textMultiLine|textCapWords"
            android:lines="12"
            android:maxLength="1000"
            android:maxLines="12"
            android:minLines="1"
            android:textColor="@color/grey_12"
            app:layout_constraintTop_toTopOf="parent" />

        <View
            android:id="@+id/tripNotes_pictureGrid"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tripNotes_editText"
            app:layout_constraintVertical_bias="0"
            app:layout_constraintVertical_weight="1"/>

    </android.support.constraint.ConstraintLayout>

</LinearLayout>

Фактический результат заключается в том, что другое представление отталкивается от экрана, если количество строк в EditText не ограничено, но я бы предпочел решение с атрибутами alyout.

1 Ответ

0 голосов
/ 05 апреля 2019

Вы можете заключить редактируемый текст в представление прокрутки

Ваш макет будет выглядеть так:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"   
    android:orientation="vertical">

    <include
        android:id="@+id/tripNotes_toolbar_layout"
        layout="@layout/toolbar_details" />

    <android.support.constraint.ConstraintLayout
        android:id="@+id/tripNotes_constraintLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/CardStyle"

        android:layout_gravity="top"
        android:layout_marginLeft="@dimen/card_outer_margin_left"
        android:layout_marginRight="@dimen/card_outer_margin_left">

        <ScrollView
            android:id="@+id/tripNotes_editTextContainer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/tripNotes_pictureGrid">
            <EditText
                android:id="@+id/tripNotes_editText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:layout_margin="8dp"
                android:background="@null"
                android:hint="Put your notes here"
                android:inputType="textMultiLine|textCapWords"
                android:textColor="@color/grey_12" />
        </ScrollView>
        <View
            android:id="@+id/tripNotes_pictureGrid"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/tripNotes_editTextContainer"
            app:layout_constraintTop_toBottomOf="@id/tripNotes_editTextContainer"
            app:layout_constraintVertical_bias="0"
            app:layout_constraintVertical_weight="1"/>

    </android.support.constraint.ConstraintLayout>

</LinearLayout>

Изменения сделаны:

  • Высота макета ограничения изменена с match_parent на wrap_content
  • Редактировать текст
    • удалено app:layout_constraintTop_toTopOf="parent" (больше не требуется, поскольку оно включено в представление прокрутки)
    • удалено maxLines, lines, minLines and maxLength атрибутов
  • Добавлен ScrollView
  • Вид снизу:
    • изменен app:layout_constraintStart_toStartOf="parent" до app:layout_constraintStart_toEndOf="@id/tripNotes_editTextContainer"
    • изменен app:layout_constraintTop_toBottomOf="@id/tripNotes_editText" до app:layout_constraintTop_toBottomOf="@id/tripNotes_editTextContainer"

Вид снизу будет находиться непосредственно под текстом редактирования и будет опускаться до тех пор, пока нижняя часть экрана и текст редактирования не станет прокручиваемым

...