Элементы AutoCompleteTextView DropDown в TextInputLayout становятся маленькими во фрагментах с помощью OutlinedBox.ExposedDropdownMenu - PullRequest
1 голос
/ 04 августа 2020

Я использую AutoCompleteTextView, TextInputLayout, ExposedDropdownMenu из Material Design вместе, и всякий раз, когда я пытаюсь использовать его внутри Fragment, элементы DropDown становятся маленькими.

Макет:

<com.google.android.material.textfield.TextInputLayout
                android:id="@+id/responsibility_layout"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="20dp"
                android:layout_marginLeft="20dp"
                app:layout_constraintEnd_toStartOf="@+id/type_of_transaction"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:startIconDrawable="@drawable/ic_baseline_person_pin_circle_24">

                <AutoCompleteTextView
                    android:id="@+id/res_input"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@null"
                    android:hint="@string/your_respon"
                    android:maxLines="1" />

            </com.google.android.material.textfield.TextInputLayout>

Kotlin сторона (используя эти коды внутри метода onViewCreated()):

val items = listOf(
        resources.getString(R.string.own_first),
        resources.getString(R.string.own_two))
        val adapter = ArrayAdapter(activity!!.applicationContext, R.layout.list_drop,
            items)
        (type_of_transaction.editText as? AutoCompleteTextView)?.setAdapter(adapter)

        (type_of_transaction.editText as AutoCompleteTextView).inputType = EditorInfo.TYPE_NULL
        type_of_transaction.onFocusChangeListener =
            View.OnFocusChangeListener { _: View?, hasFocus ->
                if (hasFocus) {
                    hideKeyboard(activity)
                }
            }

Доказательство:

введите описание изображения здесь

Интересно то, что коды работают на Activity. Не имел понятия, почему это происходит в Fragment.

Есть идеи, почему это происходит? Это ошибка?

1 Ответ

1 голос
/ 04 августа 2020

Проблема здесь:

 val adapter = ArrayAdapter(activity!!.applicationContext,...

Вы должны передать activity в качестве контекста вместо applicationContext. ApplicationContext не имеет темы приложения.

Используйте:

val adapter = ArrayAdapter(requireContext(),.....

с тематическим контекстом

enter image description here

with an application context:

введите описание изображения здесь

...