使用 OutlinedBox.ExposedDropdownMenu

AutoCompleteTextView DropDown items in TextInputLayout becomes small in Fragments using OutlinedBox.ExposedDropdownMenu

我正在使用 Material 设计中的 AutoCompleteTextViewTextInputLayoutExposedDropdownMenu,每当我尝试在 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.

中发生这种情况

知道为什么会这样吗?这是一个错误吗?

问题在这里:

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

您必须将 activity 作为上下文而不是 applicationContext 传递。 ApplicationContext没有应用主题。

使用:

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

有了themed-context

具有应用程序上下文: