Android Spinner 自定义文本视图不可点击
Android Spinner custom text view not clickable
为什么使用自定义文本视图会阻止 Spinner 文本和项目(但不是箭头)可点击,而 Android-prvoided 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
spinner_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"
您的代码工作正常。
为什么使用自定义文本视图会阻止 Spinner 文本和项目(但不是箭头)可点击,而 Android-prvoided 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
spinner_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"
您的代码工作正常。