如何更改android spinner popupBackground
我试图通过设置 android:popupBackground 来更改 android spinner 弹出窗口背景,但它没有任何效果。我有什么办法可以改变它吗?
<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">
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">
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您正在尝试更改 Spinner 弹出窗口的“外部”背景,而不是 Spinner“pupup items”的背景。另外,我认为弹出窗口是指 Spinner 的对话框模式,其中浮动对话框窗口出现在您的活动顶部,而不是新的下拉模式。
策略
对于一种在多个 Android 平台上表现良好并减少应用程序中冗余需求的干净、可持续的方法,我认为了解官方文档没有告诉我们的内容至关重要。那么请跟随我进行一次短途旅行。
Spinners 的棘手部分是使用适配器将它们连接到数据。虽然识别用于更改 android.R.layout.simple_spinner_item 及其朋友的外观的钩子相对容易,但这些钩子仅确定 Spinner 当前显示的项目以及每个弹出项目的样式。但适配器负责创建ListView和其他构成该ListView的小部件。
这种复杂性可能就是 Android 引入了一些可以为 Spinner 动态指定的属性的原因,尽管它们随后会应用于 Spnner 的子级,例如 android:弹出背景。这不一定是一种干净的方法,而是一组有限的便利功能。关于 popupBackground,顺便说一句,这是在 API 级别 1 中引入的,但 Spinners 仅在 spinnerMode=dropdown 中尊重它,这是在 API 级别 11 中引入的。这就是为什么如果您错误地使用它。
较旧的 Android 版本(例如 2.2)
ListView
知道 Adapter 创建一个 ListView,在主题中更改 ListView 外观并不是一个坏主意,因此只有一个地方对于设计更改和样式简单明了,如下所示:
AlertDialog
不幸的是,还有更多工作要做。因为 android:prompt 可用于为弹出窗口创建标题,所以弹出窗口实际上不仅仅包含 ListView。
Android 使用 AlertDialog
最新的 Android 版本(例如 4.2)
现在 AlertDialog 的样式已经确定,我们仍然需要解决以下问题:较新版本的 Android 不再使用 AlertDialogs 作为 Spinner 对话框。这并没有什么坏处,因为对于那些人来说,AlertDialog 样式无论如何都应该保留。这只是意味着我们还需要设计新弹出窗口的样式。
为此,请创建特定于版本的主题 XML 文件以将其他样式拉入您的自定义主题,并提供特定于版本的样式 XML 文件。
想从这里开始自己尝试一下吗?
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:
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?
android:popupBackground
仅在使用android:spinnerMode = "dropdown"
时有效,这可能就是为什么它在您的代码中没有任何效果的原因。您需要通过一些 XML 告诉微调器它处于哪种模式。链接
android:popupBackground
is only valid when usingandroid: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.Links