Android 样式操作栏下拉菜单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11479186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:47:17  来源:igfitidea点击:

Styling ActionBar dropdown menu

androidandroid-theme

提问by lujop

I'm using a custom theme that inherits from DarkActionBarand I want to customize dropdown menu to be white like when using Light Holo theme.

我正在使用继承自的自定义主题,DarkActionBar并且我想将下拉菜单自定义为白色,就像使用 Light Holo 主题时一样。

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.

但我不知道如何将文本颜色更改为黑色。因为在设置白色可绘制之后,问题是文本不可见,因为在白色背景上是白色的。

回答by lujop

I answer myself after some investigation. In addition to question's styling you need to:

经过一番调查,我自己回答。除了问题的样式,您还需要:

  • Customize android:spinnerDropDownItemStylefor actionBarWidgetThemechanging 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.
  • 自定义android:spinnerDropDownItemStyleactionBarWidgetTheme更改其文本外观。
  • 另外不要忘记下拉列表由您使用的适配器管理。那么如果你使用标准的 ( simple_dropdown_item_1line) 就没有问题了。但是,如果您使用像我这样的自定义(为了能够添加图标),请不要忘记style="?attr/spinnerDropDownItemStyle"在您的布局中应用 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>

Where drawables in myapp_DropDownNavare white background ones that you can generate with ActionBar Style generatorin Android Asset Studio

其中 drawablemyapp_DropDownNav是白色背景的,您可以在Android Asset Studio 中使用ActionBar 样式生成器生成它们

回答by Jason Robinson

Try setting itemTextAppearance. That should achieve what you want.

尝试设置itemTextAppearance。那应该达到你想要的。

回答by Georgie

I have stumbled on what may be the simplest way to do this. I was working with the AppCompat library.

我偶然发现了可能是最简单的方法。我正在使用 AppCompat 库。

<style name="ApplicationTheme" parent="@style/Theme.AppCompat">
    <item name="android:actionBarWidgetTheme">@style/Theme.AppCompat.Light</item>
    <item name="actionBarWidgetTheme">@style/Theme.AppCompat.Light</item>
</style>

回答by toadzky

My advice is to simply inherit from the Sherlock.Light theme and change the applicable fields to the Dark values. For my app, we wanted a white "up" icon, and white text for the action labels. I don't provide dark versions of my actionbar icons, so they are all white anyway. So after several hours messing around with it and following different people's suggestions, I finally found what I was looking for in the ABS themes file.

我的建议是简单地从 Sherlock.Light 主题继承并将适用的字段更改为 Dark 值。对于我的应用程序,我们想要一个白色的“向上”图标,以及用于操作标签的白色文本。我不提供我的操作栏图标的深色版本,所以无论如何它们都是白色的。所以在折腾了几个小时并听从了不同人的建议之后,我终于在 ABS 主题文件中找到了我想要的东西。

I inherit from Sherlock.Light (well, technically HoloEverywhereLight.Sherlock but...) and change:

我从 Sherlock.Light 继承(好吧,技术上是 HoloEverywhereLight.Sherlock 但是...)并更改:

<item name="android:actionMenuTextColor">@color/White</item>
<item name="actionMenuTextColor">@color/White</item>
<item name="android:homeAsUpIndicator">@drawable/abs__ic_ab_back_holo_dark</item>
<item name="homeAsUpIndicator">@drawable/abs__ic_ab_back_holo_dark</item>
<item name="android:dividerVertical">@drawable/abs__list_divider_holo_dark</item>
<item name="dividerVertical">@drawable/abs__list_divider_holo_dark</item>

That's it. It's way simpler and easier than trying to extend classes, restyle things in code, etc.

就是这样。它比尝试扩展类、在代码中重新设置样式等更简单、更容易。