android - 如何更改 android spinner popupBackground

标签 android android-widget android-spinner

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

    <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 弹出窗口的“外部”背景,而不是 Spinner“pupup items”的背景。另外,我认为弹出窗口是指 Spinner 的对话模式,其中 float 对话窗口出现在您的 Activity 之上,而不是新的下拉模式。

策略

为了获得一种在多个 Android 平台上表现良好并减少应用程序冗余需求的干净、可持续的方法,我认为了解官方文档没有告诉我们的内容至关重要。所以跟我来个短途旅行吧。

微调器的棘手之处在于,适配器 用于将它们连接到数据。虽然识别用于更改 android.R.layout.simple_spinner_item 及其 friend 的外观的钩子(Hook)相对容易,但它们仅确定 Spinner 当前显示的项目以及每个弹出项的样式。但是 Adapter 负责创建 ListView 和构成该 ListView 的其他小部件。

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

较旧的 Android 版本(例如 2.2)

ListView

知道 Adapter 创建了一个 ListView,在一个主题中更改 ListView 的外观并不是一个坏主意,因此设计更改和样式简单明了,像这样:

<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>

警报对话框

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

Android 使用一个 AlertDialog

最近的 Android 版本(例如 4.2)

既然 AlertDialogs 的样式已经设置好,我们仍然必须解决一个事实,即较新版本的 Android 不再将 AlertDialogs 用于 Spinner 对话框。这并没有什么坏处,因为对于那些人来说,无论如何都应该保留 AlertDialog 样式。这只是意味着我们还需要设置新弹出窗口的样式。

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

想从这里开始自己尝试吗?

关于android - 如何更改 android spinner popupBackground,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8922924/

相关文章:

android - 共享 apklib 文件但隐藏源代码可能吗?

安卓蓝牙 : how to detect which slave is disconnected when there are multiple slaves?

android - 如何在不重建所有 RemoteViews 的情况下部分更新应用程序小部件中的 View

Android 2.2 更新小部件对我永远不起作用

滚动时 Android ListView 样式问题与背景

java - 如何在 Android 中跟踪有关 fragment 的信息

android - 该应用程序不会在 nougat api 7++ 上获得本地化更改效果

android - 找到所有匹配标签的微调器

java - 我不明白这段代码

android - 不要在 onRestoreInstanceState 上运行函数