在Android 5.0中设置弹出菜单的样式

时间:2014-11-02 12:52:34

标签: android android-ui android-5.0-lollipop material-design android-support-library

我正在为Android 5.0准备我的应用程序,我正在使用最新的兼容性库,这就是我的风格。

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/theme_accent</item>
        <item name="colorAccent">@color/theme_accent_secondary</item>
    </style>

    <style name="AppThemeDark" parent="Theme.AppCompat">
        <item name="colorPrimary">@color/theme_accent</item>
        <item name="colorAccent">@color/theme_accent_secondary</item>
    </style>
</resources>

(ActionBar颜色以编程方式设置。)

现在,我希望溢出/弹出菜单具有类似于holo实现中的深色背景,但我无法使其工作,这是它的样子:enter image description here

我已尝试设置popupMenuStyle,但它无效。

如何让弹出菜单变暗?

5 个答案:

答案 0 :(得分:20)

停止使用ActionBar。如果您希望将ToolBar设置为ActionBar,请在android-developers博客上关注this guide

它实际上在 Dark Action Bar 中提及了您的用例并提供了此代码:

<android.support.v7.widget.Toolbar
    android:layout_height=”wrap_content”
    android:layout_width=”match_parent”
    android:minHeight=”@dimen/triple_height_toolbar”
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

答案 1 :(得分:10)

不是一个完整的答案,但到目前为止我发现了:

在以前的版本中,您需要指定一个drawable(检查https://github.com/StylingAndroid/StylingActionBar代码和教程)

显然,现在这是一种颜色。要修改它,您需要指定以下主题:

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="AppTheme" parent="android:Theme.Material.Light.DarkActionBar">
        <item name="android:actionBarPopupTheme">@style/popupNew</item>
    </style>

    <style name="popupNew" parent="android:ThemeOverlay.Material.Light">
        <item name="android:colorBackground">@color/red</item>
    </style>

</resources>

如果应用于应用的主题就是这个,这可以正常工作。 如果我将android:actionBarPopupTheme添加到现有主题中,则无效。我想弄明白为什么。

答案 2 :(得分:7)

使用此样式解决了我的问题:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/theme_accent</item>
    <item name="colorAccent">@color/theme_accent_secondary</item>
    <item name="actionBarStyle">@style/AbStyle</item>
    <item name="actionModeBackground">@color/actionmode_bg</item>
</style>

<style name="AbStyle" parent="Widget.AppCompat.Toolbar">
    <item name="elevation">2dp</item>
    <item name="displayOptions">homeAsUp|showTitle</item>
    <!--showHome-->
</style>

<style name="AppThemeDark" parent="Theme.AppCompat">
    <item name="colorAccent">@color/theme_accent_secondary</item>
    <item name="actionBarStyle">@style/AbStyle</item>
</style>

我不得不使用Widget.AppCompat.Toolbar作为父actionBarStyle

答案 3 :(得分:6)

将属性popupTheme添加到工具栏:

<android.support.v7.widget.Toolbar
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:id="@+id/toolbar"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="@color/color_primary"
  app:theme="@style/Theme.AppCompat.Light"
  app:popupTheme="@style/Theme.AppCompat" />

或者为工具栏定义新样式:

<style name="MyToolBarStyle" parent="Widget.AppCompat.Toolbar">
    <item name="android:background">@color/green</item>
    <item name="popupTheme">@style/Theme.AppCompat.Light</item>
    <item name="theme">@style/Theme.AppCompat</item>
</style>

答案 4 :(得分:1)

这个问题已经通过XML回答了样式,但是我在这里添加了一个解释,说明如何自己解决这个问题和类似的样式问题。

首先,这是使用AppCompat时的解决方案。在App的style.xml中添加actionBarPopupTheme到你的主题:

<style name="Theme.MyTheme" parent="@style/Base.Theme.AppCompat.Light.DarkActionBar">
    ...other stuff here

    <item name="actionBarPopupTheme">@style/Theme.MyTheme.ActionBarPopupTheme</item>
</style>

<style name="Theme.MyTheme.ActionBarPopupTheme" parent="@style/ThemeOverlay.AppCompat.Light">
    <item name="android:textColor">@android:color/white</item>
    <item name="android:background">@android:color/black</item>
</style>

以下是我为达成此解决方案所采取的步骤(因为Android文档很差,需要一些侦探工作):

  • 在Android Studio中打开应用程序的style.xml
  • 在定义App主题的行上,将屏幕光标放在父主题中(例如,单击@ style / Base.Theme.AppCompat.Light.DarkActionBar),然后按F4。这将带您进入appcompat库中样式的源代码。
  • 在这种风格中,我看到了这一行:

    &LT; item name =&#34; actionBarPopupTheme&#34;&gt; @ style / ThemeOverlay.AppCompat.Light &lt; /项目&GT;

    这看起来像是一个可能改变弹出窗口主题的地方。我搜索了&#34; actionBarPopupTheme&#34;在穷人       Android开发人员文档并找到&#34;参考应该使用的主题       通过操作栏中的小部件显示弹出窗口&#34;。所以这值得玩。

  • 我复制了包含&#34; actionBarPopupTheme&#34;的appcompat行。到我的style.xml然后在这一行用Theme.MyTheme.ActionBarPopupTheme替换了项目的主题参考(上面的粗体位)。

  • 在我的style.xml中,我创建了名为Theme.MyTheme.ActionBarPopupTheme的新样式。我使用了从appcompat源复制的样式中使用的相同父级(上面的粗体位)。
  • 为确保我的新弹出式样式正常工作,我将父样式更改为ThemeOverlay.AppCompat.Dark,然后在设备上运行并测试代码。弹出的样式发生了变化,所以现在我知道我的重写actionBarPopupTheme是正确的做法。然后我改回了ThemeOverlay.AppCompat.Light。
  • 接下来的挑战是找出要在Theme.MyTheme.ActionBarPopupTheme中覆盖的项目名称。我改变了文字和背景颜色。在某些情况下,要找到改变某种风格的正确项目名称可能会非常棘手。查找不太明显的样式项名称的一种方法是查看appcompat xml文件中的样式定义(在上面的第2步中按F4时打开的样式定义),不断下降到父样式(F4再次!),直到找到某些内容这可能会做你想要的。谷歌搜索也会有所帮助。