如何更改Android Spinner PopupBackground [英] how to change android spinner popupBackground

查看:485
本文介绍了如何更改Android Spinner PopupBackground的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过设置android:popupBackground来更改android微调框弹出窗口的背景,但是没有任何效果.有什么办法可以改变吗?

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?

    <Spinner
           android:id="@+id/eventNameSpinner"
           android:layout_width="160dp"
           android:layout_height="30dp"
           android:layout_alignParentLeft="true"
           android:layout_centerVertical="true"
           android:layout_marginLeft="6dp"
           android:background="@drawable/btn_name"
           android:paddingBottom="2dp"
           android:paddingTop="2dp"
           android:popupBackground="@drawable/bkg">

推荐答案

我认为您正在尝试更改微调框弹出窗口的外部"背景,而不是微调框弹出项"的背景.另外,我假设通过弹出窗口来表示Spinner的对话模式,其中浮动对话框窗口出现在活动顶部,而不是新的下拉模式.

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.

对于一个干净,可持续的方法,该方法在多个Android平台上均能很好地运行并减少App中的冗余需求,我认为了解官方文档没有告诉我们的内容至关重要.所以跟随我走一小段路程.

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.

Spinners的棘手部分是使用 Adapter 将它们连接到数据.虽然识别用于更改android.R.layout.simple_spinner_item及其朋友外观的挂钩相对容易,但这些挂钩仅确定Spinner当前显示的项目以及每个单个弹出项目的样式.但是,是 Adapter 负责创建 ListView 和其他构成该ListView的小部件.

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.

这种复杂性可能是Android引入了一些可以即时为 Spinner 指定的属性的原因,尽管这些属性随后被应用于Spnner的子级,例如 android:popupBackground .这不一定是一种干净的方法,而是有限的一组便利功能.关于popupBackground,顺便说一句,这是在API级别1中引入的,但是Spinners仅在API级别11中引入的 spinnerMode = dropdown 中才尊重它.这就是为什么如果您从未收到通知的原因,错误使用它.

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.

知道 Adapter 创建了 ListView ,更改主题主题中的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

不幸的是,还有更多工作要做.因为android:prompt可用于为弹出窗口创建标题,所以该弹出窗口实际上不仅包含ListView,而且还包含其他内容.

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使用AlertDialog

Android uses an AlertDialog

现在,AlertDialogs的样式已设置,我们仍然必须解决以下事实:较新版本的Android不再将AlertDialogs用于Spinner对话框.这并没有什么坏处,因为对于那些人而言,应该始终保持AlertDialog样式.这只是意味着我们还需要设置新弹出窗口的样式.

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.

为此,请创建特定于版本的主题XML文件,以将其他样式引入您的自定义主题,并提供特定于版本的样式XML文件.

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?

这篇关于如何更改Android Spinner PopupBackground的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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