如何自定义菜单项的背景颜色?

时间:2016-09-07 06:34:13

标签: android menuitem android-appcompat android-toolbar

我正在尝试自定义Toolbar的弹出菜单。现在我无法设置菜单项的背景颜色。

我的styles.xml如下所示:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme.NoActionBar" parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>

        <item name="android:actionBarStyle">@style/AppTheme.ActionBar</item>
        <item name="android:actionMenuTextColor">@color/actionbar_text_color</item>
        <item name="android:dropDownListViewStyle">@style/AppTheme.DropDown</item>
        <item name="android:itemTextAppearance">@style/menuItemColor</item>

        <!-- Support library compatibility -->
        <item name="actionBarStyle">@style/AppTheme.ActionBar</item>
        <item name="actionMenuTextColor">@color/actionbar_text_color</item>
        <item name="dropDownListViewStyle">@style/AppTheme.DropDown</item>
        <item name="itemTextAppearance">@style/menuItemColor</item>
    </style>

    <style name="AppTheme.ActionBar" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">\
        <item name="titleTextColor">@color/actionbar_text_color</item>
        <item name="subtitleTextColor">@color/actionbar_text_color</item>

        <item name="android:background">@color/actionbar_background</item>

        <!-- Support library compatibility -->
        <item name="background">@color/actionbar_background</item>
    </style>

    <style name="AppTheme.DropDown" parent="@style/Widget.AppCompat.Light.ListView.DropDown">
        <item name="android:background">@color/background_color</item>
        <!-- Support library compatibility -->
        <item name="background">@color/background_color</item>
    </style>

    <style name="menuItemColor">
        <item name="android:textColor">@color/inactive_text_color</item>
    </style>

</resources>

我使用代码

将ActionBar添加到活动布局中
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:theme="@style/AppTheme.ActionBar"/>

注意:我使用的是AppCompat,而不是Holo或其他。

2 个答案:

答案 0 :(得分:0)

这可以帮助你,创建一个像

这样的风格
<style name="PopupMenu" parent="@android:style/Widget.PopupMenu">
<item name="android:popupBackground">@android:color/white</item>

并将其添加到主要主题..

 <style name="AppTheme.NoActionBar" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
<item name="android:popupMenuStyle">@style/PopupMenu</item> <! added it here
    <item name="android:actionBarStyle">@style/AppTheme.ActionBar</item>
    <item name="android:actionMenuTextColor">@color/actionbar_text_color</item>
    <item name="android:textColor">@color/text_color</item>

    <!-- Support library compatibility -->
    <item name="actionBarStyle">@style/AppTheme.ActionBar</item>
    <item name="actionMenuTextColor">@color/actionbar_text_color</item>
</style>

<style name="AppTheme.ActionBar" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">\
    <item name="titleTextColor">@color/actionbar_text_color</item>
    <item name="subtitleTextColor">@color/actionbar_text_color</item>
<item name="android:popupMenuStyle">@style/PopupMenu</item> <! added it here
    <item name="android:background">@color/actionbar_background</item>

    <!-- Support library compatibility -->
    <item name="background">@color/actionbar_background</item>
</style>

答案 1 :(得分:0)

您必须为溢出菜单定义样式,如下所示:

<!-- OverFlow menu Styles -->
<style name="PopupMenuListView" parent="@style/Widget.AppCompat.Light.ListView.DropDown">
    <item name="android:divider">@color/sp_red_500</item>
    <item name="android:dividerHeight">1dp</item>
    <item name="android:background">@color/white</item>
</style>

并在您的父主题样式(基本应用程序主题。)中定义下拉菜单样式,如下所示:

 <item name="dropDownListViewStyle">@style/PopupMenuListView</item>