how to change android spinner popupBackground

后端 未结 2 330
孤城傲影
孤城傲影 2021-01-18 04:18

I was trying to change the android spinner popup window background by setting the android:popupBackground, but it didn\'t have any effect. Is there any way I can change it?

相关标签:
2条回答
  • 2021-01-18 04:56

    I presume you are trying to change the "outer" backgroud of a Spinner's popup, not the background of Spinner "pupup items". Also, I presume that by popup, you mean a Spinner's dialog mode, where a floating dialog window appears on top of your activity, as opposed to the new dropdown mode.

    Strategy

    For a clean, sustainable approach which behaves well across multiple Android platforms and reduces the need for redundancy in the App, I believe it is essential to understand what the official docs don't tell us. So follow me on a short journey.

    The tricky part about Spinners is that an Adapter is used to connect them to data. While it is relatively easy to identify the hooks for changing the appearance of android.R.layout.simple_spinner_item and its friends, those only determine the style of the Spinner's currently displayed item as well as each single popup item. But it is the Adapter which is responsible for creating the ListView and other widgets which frame that ListView.

    This complexity is probably the reason why Android has introduced some attributes which can be specified on-the-fly "for" the Spinner although they are then applied to the Spnner's children, such as android:popupBackground. This is not necessarily a clean approach, rather a limited set of convenience functions. Regarding popupBackground, btw, this was introduced in API level 1, but Spinners respect it only in spinnerMode=dropdown, which was introduced in API level 11. That's the reason why you'll never be notified if you use it wrongly.

    Older Android Versions (such as 2.2)

    ListView

    Knowing that the Adapter creates a ListView, it's not a bad idea to change the ListView appearance in one's theme, so there's one single place for the design change and the styling straightforward, like so:

    <style name="MyTheme" parent="@android:style/[reference to original theme]" >
        <item name="android:listViewStyle">@style/myListView</item>
        [...]
    </style>
    
    <style name="myListView" parent="@android:style/Widget.ListView">
        [check Android's Widget.ListView to understand what you can change here]
    </style>
    

    AlertDialog

    Unfortunately, there's more work ahead. Because android:prompt can be used to create a headline for the popup, the popup really consists of more than just the ListView.

    Android uses an AlertDialog

    Recent Android Versions (such as 4.2)

    Now that the AlertDialogs are styled, we still have to address the fact that more recent versions of Android don't use AlertDialogs for Spinner dialogs any more. That doesn't hurt, because for those, the AlertDialog style shouldd be kept anyways. It just means we need to style the new popup as well.

    To do so, create version specific theme XML files to pull the additional styles into your customized theme, and provide version specific style XML files.

    Feel like trying it yourself, starting here?

    0 讨论(0)
  • 2021-01-18 05:17

    android:popupBackground is only valid when using android:spinnerMode = "dropdown" , thats probably why it wasnt any effect in your code. You need to tell that spinner which mode its in with some XML.

    android:spinnerMode = "dropdown"
    

    Links

    • http://developer.android.com/reference/android/widget/Spinner.html#attr_android:popupBackground
    • http://developer.android.com/reference/android/widget/Spinner.html
    0 讨论(0)
提交回复
热议问题