Android Spinner自定义文本视图不可单击 [英] Android Spinner custom text view not clickable

查看:176
本文介绍了Android Spinner自定义文本视图不可单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么使用自定义文本视图会阻止Spinner文本&项(但不是箭头)被单击,但是Android禁止的Spinner文本布局不是这种情况吗?

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?

使用时有效

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

科特林

    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"

您的代码可以正常工作.

Your code works fine.

这篇关于Android Spinner自定义文本视图不可单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆