Android Spinner自定义文本视图不可点击



为什么使用自定义文本视图会阻止Spinner text&项目(但不是箭头(无法点击,但Android prvoid Spinner文本布局不是这样吗?

使用时有效

val arrayAdapter = ArrayAdapter(view!!.context, android.R.layout.simple_dropdown_item_1line, spinnerItems)
arrayAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line)

XML

<Spinner
android:id="@+id/mySpinner"
style="@style/Widget.AppCompat.Spinner.Underlined"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:spinnerMode="dialog"/>

Kotlin

spinnerItems = arrayOf(
"Cathedral of the Intercession of the Most Holy Theotokos on the Moat",
"Ferapontov Monastery",
"Historic Monuments of Novgorod and Surroundings",
"Golden Mountains of Altai",
"Historic Centre of Saint Petersburg and Related Groups of Monuments",
"Bogoroditse-Smolensky Monastery",
"White Monuments of Vladimir and Suzdal"
)
val arrayAdapter = ArrayAdapter(view!!.context, R.layout.spinner_item, spinnerItems)
arrayAdapter.setDropDownViewResource(R.layout.spinner_item)
mSpinner.adapter = arrayAdapter

spiner_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/dropDownItemStyle"
android:id="@+id/my_SpinnerItem"
android:background="?android:attr/selectableItemBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:textColor="?android:attr/textColorPrimary" />

删除这两行:

android:clickable="true"
android:focusable="true"

您的代码运行良好。

最新更新