2015-11-02 62 views
0

如何更改菜单中背景的颜色?它默认是白色的。我更改了style.xml和菜单,但它仍然显示白色菜单。 是什么问题,此代码: 我可以删除如何更改菜单中背景的颜色?

android:theme="@style/your_custom_optionsMenuStyle" 

形式的manifest.xml和菜单的颜色变成黑色。但我想知道这是好方法吗?并且在具有不同API的其他设备中是黑色的?

<item 
     android:id="@+id/menu_settings" 
     android:title="@string/menu_settings" 
     android:orderInCategory="100"/> 
     <item name="actionOverflowMenuStyle">@style/your_custom_optionsMenuStyle</item> 

</menu> 

style.xml文件: <

<!-- 
    Base application theme, dependent on API level. This theme is replaced 
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices. 
--> 
<style name="AppBaseTheme" parent="android:Theme.Light"> 
    <!-- 
     Theme customizations available in newer API levels can go in 
     res/values-vXX/styles.xml, while customizations related to 
     backward-compatibility can go here. 
    --> 
</style> 

<!-- Application theme. --> 
<style name="AppTheme" parent="AppBaseTheme"> 
    <!-- All customizations that are NOT specific to a particular API-level can go here. --> 
</style> 



<style name="your_custom_optionsMenuStyle" parent="AppBaseTheme"> 
<item name="android:popupBackground">#000000</item> 
</style> 

</resources> 

清单:

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/your_custom_optionsMenuStyle" 
     > 
+0

的可能的复制[如何更改背景颜色弹出式菜单中的Android ](http://stackoverflow.com/questions/21231404/how-to-change-background-color-popup-menu-android) –

+0

你有几个零? https://en.wikipedia.org/wiki/Web_colors#Hex_triplet –

+0

[如何更改ActionBar菜单背景颜色(android 4&5)](http://stackoverflow.com/questions/28974855/how-to -change-actionbar-menu-background-color-android-4-5) – camelCaseCoder

回答

2

您可以从款式的xml文件使用属性

<item name="actionOverflowMenuStyle">@style/your_custom_optionsMenuStyle</item> 
覆盖的菜单风格在你的应用程序的主题

其中your_custom_optionsMenuStyle指定每个菜单属性

<style name="your_custom_optionsMenuStyle" parent="Widget.AppCompat.PopupMenu.Overflow"> 
    <item name="android:popupBackground">your color code</item> 
</style> 
+0

所以它应该将api更改为21.有什么方法可以不更改api? – user5508330

+0

您可以更改父标记中的窗口小部件引用以指向您在当前API中使用的相应窗口小部件 – NIPHIN

+0

我更改了代码并将其添加到上面的问题中。请认真告诉我你的想法。 – user5508330