首页 > 解决方案 > Kotlin - 以编程方式创建的 Spinner 箭头丢失

问题描述

以编程方式创建后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(view!!.context, null, android.R.style.Widget_Material_Spinner, Spinner.MODE_DROPDOWN)

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

        mySpinner.adapter = arrayAdapter

        mFrameLayout.addView(mySpinner)

在此处输入图像描述

标签: androidkotlinandroid-spinnerandroid-dialog

解决方案


这是因为您为微调器选择的样式

android.R.style.Widget_Material_Spinner

与默认行为不同,此样式中不包含箭头。您需要将其替换为其他样式或自己添加箭头。

val mySpinner = Spinner(
        ContextThemeWrapper(this, R.style.Widget_AppCompat_Spinner_Underlined),
        null,
        0,
        Spinner.MODE_DROPDOWN
    )

尝试这个


推荐阅读