【问题标题】:Kotlin - Programmatically created Spinner dropdown shows underline and arrow in dropdownKotlin - 以编程方式创建的 Spinner 下拉菜单在下拉菜单中显示下划线和箭头
【发布时间】:2020-04-24 13:37:06
【问题描述】:

为什么每当我为我的Spinner 使用自定义文本视图时,会出现这种奇怪的下拉行为?出于某种原因,下拉菜单似乎正在获取可绘制的下划线和箭头,这是我从未要求过的。这个怎么去掉?

片段

        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 mySpinner = Spinner(ContextThemeWrapper(view!!.context, R.style.Widget_AppCompat_Spinner_Underlined),
            null,
            0,
            Spinner.MODE_DROPDOWN
        )

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

        mySpinner.adapter = arrayAdapter

        mFrameLayout.addView(mySpinner)

自定义下拉项

<TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        style="?android:attr/dropDownItemStyle"
        android:id="@+id/my_spinner_item"
        android:background="?android:attr/selectableItemBackground"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true" />

【问题讨论】:

  • 能否将tv_spinner_itemmy_spinner_item 布局文件添加到问题中?

标签: android kotlin android-spinner


【解决方案1】:

好的,我设法找到了相当便宜的解决方案,但它似乎有效。只需将您的微调器创建逻辑放在 xml 中,这样说:

<Spinner 
     xmlns:android="http://schemas.android.com/apk/res/android"
     style="@style/Widget.AppCompat.Spinner.Underlined"
     android:id="@+id/mySpinner"
     android:layout_width="match_parent"
     android:layout_height="wrap_content" />

然后在你的布局中从代码中膨胀它,你可以从这样的活动中做到这一点:

    layoutInflater.inflate(R.layout.item, frameLayout)

    val arrayAdapter = ArrayAdapter(this, R.layout.support_simple_spinner_dropdown_item, spinnerItems)
    arrayAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item)

    mySpinner.adapter = arrayAdapter

【讨论】:

  • 酷,但现在我无法点击任何项目。我问过a new question
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-27
  • 1970-01-01
  • 2013-10-10
  • 2015-07-11
  • 2014-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多