样式操作栏下拉菜单 [英] Styling ActionBar dropdown menu
问题描述
我正在使用从 DarkActionBar
继承的自定义主题,我想将下拉菜单自定义为白色,就像使用 Light Holo 主题一样.
I'm using a custom theme that inherits from DarkActionBar
and I want to customize dropdown menu to be white like when using Light Holo theme.
我已经能够使用以下方法将背景更改为白色:
I've been able to change the background to white using:
<style name="MyTheme" parent="@style/Theme.Light.DarkActionBar">
<item name="android:actionDropDownStyle">@style/MyDropDownNav</item>
</style>
<style name="MyDropDownNav">
<item name="android:background">@drawable/spinner_background_white</item>
<item name="android:popupBackground">@drawable/menu_dropdown_panel_whyite</item>
<item name="android:dropDownSelector">@drawable/selectable_background_white</item>
</style>
但我不知道如何将文本颜色更改为黑色.因为设置白色可绘制后,问题是文本不可见,因为在白色背景上是白色的.
But I haven't any clue of how to change the text color to black. Because after setting white drawable the problem is that text isn't visible because is white on white background.
推荐答案
经过一番调查,我自己回答.除了问题的样式,您还需要:
I answer myself after some investigation. In addition to question's styling you need to:
- 为
actionBarWidgetTheme
自定义android:spinnerDropDownItemStyle
改变它的文本外观. - 另外不要忘记下拉列表是由您使用的适配器管理的.然后,如果您使用标准的 (
simple_dropdown_item_1line
),则没有问题.但是,如果您使用像我这样的自定义(为了能够添加图标)不要忘记应用style="?attr/spinnerDropDownItemStyle"
在您的布局 TextView.
- Customize
android:spinnerDropDownItemStyle
foractionBarWidgetTheme
changing it's text appearance. - Also don't forget that dropdown list is managed by the adapter you use. Then if you used the standard one (
simple_dropdown_item_1line
) there's no problem. But if you used a custom one like me (to be able to add an icon) don't forget to applystyle="?attr/spinnerDropDownItemStyle"
in your layout TextView.
那么最终的自定义样式是:
Then final custom style is:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Theme.myapp" parent="@style/Theme.Light.DarkActionBar">
<item name="android:actionDropDownStyle">@style/myapp_DropDownNav</item>
<item name="android:actionBarWidgetTheme">@style/myapp.actionBarWidgetTheme</item>
</style>
<style name="myapp.actionBarWidgetTheme" parent="@style/Theme.">
<item name="android:spinnerDropDownItemStyle">@style/myapp.Widget.DropDownItem.Spinner</item>
</style>
<style name="myapp_DropDownNav" parent="@style/Widget.Spinner.DropDown.ActionBar">
<item name="background">@drawable/spinner_background_ab_myapp</item>
<item name="android:background">@drawable/spinner_background_ab_myapp</item>
<item name="android:popupBackground">@drawable/menu_dropdown_panel_myapp</item>
<item name="android:dropDownSelector">@drawable/selectable_background_myapp</item>
</style>
<style name="myapp.Widget.DropDownItem.Spinner" parent="Widget.DropDownItem.Spinner">
<item name="android:textAppearance">@style/myapp.TextAppearance.Widget.DropDownItem</item>
</style>
<style name="myapp.TextAppearance.Widget.DropDownItem" parent="TextAppearance.Widget.DropDownItem">
<item name="android:textColor">@color/black</item>
</style>
myapp_DropDownNav
中的 drawable 是白色背景的,您可以使用 ActionBar 生成Android Asset Studio>
Where drawables in myapp_DropDownNav
are white background ones that you can generate with ActionBar Style generator in Android Asset Studio
这篇关于样式操作栏下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!