2015-11-02 101 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