【问题标题】:Kotlin - Programmatically created Spinner arrow missingKotlin - 以编程方式创建的 Spinner 箭头丢失
【发布时间】:2020-04-24 19:43:14
【问题描述】:

以编程方式创建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)

【问题讨论】:

    标签: android kotlin android-spinner android-dialog


    【解决方案1】:

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

    android.R.style.Widget_Material_Spinner
    

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

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

    试试这个

    【讨论】:

    • 哪种样式默认有箭头?
    • 根本不声明样式 val mySpinner = Spinner(this, Spinner.MODE_DROPDOWN),这样就可以了
    • 解决了。你知道如何使用像this one 这样的下划线样式吗?在 XML 中,我曾经使用 style="@style/Widget.AppCompat.Spinner.Underlined",但这完全可以通过编程方式完成吗?
    • 很好,但现在这引起了其他问题,我找不到类似的票。我在new question 中问过这个问题。
    • 我看到了你的帖子,我一直在尝试解决它,但它变得越来越奇怪,我猜 spinner api 有问题?无论如何,我有一些想法,如果我设法解决它,我会联系你!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    • 1970-01-01
    • 2018-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多