【问题标题】:Android Spinner custom text view not clickableAndroid Spinner 自定义文本视图不可点击
【发布时间】:2020-04-24 22:01:02
【问题描述】:

为什么使用自定义文本视图会阻止 Spinner 文本和项目(但不是箭头)可点击,而 Android 提供的 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"/>

科特林

    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中相同的idXML
  • @OhhhThatVarun 你能再具体一点吗? id 什么?

标签: android kotlin android-spinner


【解决方案1】:

删除这两行:

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

您的代码运行良好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-12
    • 1970-01-01
    • 2018-09-14
    • 1970-01-01
    • 2016-09-06
    • 1970-01-01
    相关资源
    最近更新 更多