Kotlin-以编程方式创建的Spinner下拉菜单在下拉菜单中显示下划线和箭头 [英] Kotlin - Programmatically created Spinner dropdown shows underline and arrow in dropdown

查看:1279
本文介绍了Kotlin-以编程方式创建的Spinner下拉菜单在下拉菜单中显示下划线和箭头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么每当我为Spinner使用自定义文本视图时,都会出现这种奇怪的下拉行为?出于某种原因,下拉菜单似乎正在获得可绘制的下划线和箭头,而我从未要求过.如何将其删除?

Why do I get this weird dropdown behaviour whenever I use a custom text view for my Spinner? The dropdown seems to be obtaining the underline and arrow drawable for some reason, which I never asked for. How can this be removed?

片段

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

推荐答案

好吧,我设法找到了非常便宜的解决方案,但它似乎可行.只需将微调器创建逻辑放在xml中,就可以这样说:

okay I managed to find out pretty cheap solution but it seems to work. just put your spinner creation logic in xml, say like that :

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

,然后从代码中将其添加到您的布局中,您可以通过这样的活动来完成它:

and then inflate it in your layout from code, you would do it from activity like that :

    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

这篇关于Kotlin-以编程方式创建的Spinner下拉菜单在下拉菜单中显示下划线和箭头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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