我正在使用下面的代码来改变我的spinner的背景颜色。 我很困惑为什么它不能工作。
style.xml
<style name="MyTheme" parent="@style/Theme.Sherlock.Light">
<item name="windowActionBarOverlay">true</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="actionDropDownStyle">@style/MyActionBarSpinnerStyle</item>
</style>
<style name="MyActionBarSpinnerStyle" parent="@style/Widget.Sherlock.Light.Spinner.DropDown.ActionBar">
<item name="android:background">@drawable/actionbar_spinner_background</item>
</style>
actionbar_spinner_background.xml。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/spinner_background_disabled" android:state_enabled="false"/>
<item android:drawable="@drawable/spinner_background_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/spinner_background_focused" android:state_focused="true" android:state_pressed="false"/>
<item android:drawable="@drawable/spinner_background_default"/>
</selector>
我是否有什么地方遗漏了或做得不对?
我有单独的style.xml文件用于默认和11+。这是从我的11+文件中得到的。我把11+文件中的true删除了,但还是不行。在运行4.4.4的设备上测试,我使用的是Sherlock,这有什么关系吗?
问题是下面一行代码需要API级别11及以上。
<item name="android:windowActionBarOverlay">true</item>
单独制作 styles.xml
在 res/values-v11
中支持同样的功能。android 3.0
及以上。
如果您的 minSdkVersion
设置为 11 or higher
你的自定义主题应该使用 Theme.Holo
主题(或它的一个后代)作为你的父主题。
例如
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="@android:style/Theme.Holo">
<item name="android:windowActionBarOverlay">true</item>
</style>
</resources>
如果您的应用程序正在使用支持库,以便在运行低于以下版本的设备上实现兼容性。Android 3.0
你的自定义主题应该使用 Theme.AppCompat
主题(或它的一个后代)作为你的父主题。
例如
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="@android:style/Theme.AppCompat">
<item name="android:windowActionBarOverlay">true</item>
<!-- Support library compatibility -->
<item name="windowActionBarOverlay">true</item>
</style>
</resources>
注:本主题包括两个定义。
注意到这一主题包括两个定义。windowActionBarOverlay
风格:一体 android: prefix
和一个没有前缀的。有android:前缀的是针对在平台中包含该样式的Android版本,没有前缀的是针对从支持库中读取样式的旧版本。
参考资料。
https:/developer.android.comtrainingbasicsactionbaroverlaying.html#EnableOverlay。