Android Spinner custom text view not clickable

隐身守侯 提交于 2020-05-17 06:39:06

问题


Why does using a custom text view prevent the Spinner text & items (yet not the arrow) from being clickable and yet this isn't the case with Android-prvoided Spinner text layouts?

works when used

    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" />

回答1:


Remove these two lines:

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

Your code works fine.



来源:https://stackoverflow.com/questions/61418122/android-spinner-custom-text-view-not-clickable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!