首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >AutoCompleteTextView DropDown项在TextInputLayout中使用OutlinedBox.ExposedDropdownMenu变成小片段

AutoCompleteTextView DropDown项在TextInputLayout中使用OutlinedBox.ExposedDropdownMenu变成小片段
EN

Stack Overflow用户
提问于 2020-08-04 10:23:31
回答 1查看 1.4K关注 0票数 1

我把AutoCompleteTextViewTextInputLayoutExposedDropdownMenu从材料设计中一起使用,每当我试图在Fragment中使用它时,DropDown项就会变得很小。

布局:

代码语言:javascript
运行
复制
<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 side (在方法中使用这些代码):

代码语言:javascript
运行
复制
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中会发生这种情况。

知道为什么会发生这种事吗?这是个虫子吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-04 12:14:05

问题是:

代码语言:javascript
运行
复制
 val adapter = ArrayAdapter(activity!!.applicationContext,...

您必须将activity作为上下文传递,而不是applicationContextApplicationContext 并没有把应用主题放在上。

使用:

代码语言:javascript
运行
复制
val adapter = ArrayAdapter(requireContext(),.....

以主题为背景

具有应用程序上下文:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63244893

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档