2015-08-20 48 views
4

爲什麼我的styles.xml代碼成功地更改了我的操作欄溢出菜單的背景顏色,但無法更改我的應用中上下文菜單的背景顏色?android:popupBackground不適用於上下文菜單

<style name="AppBaseTheme" parent="Theme.AppCompat.Light"> 
    <!--<item name="android:actionBarStyle">@style/DarkActionBar</item> --> 

</style> 

<!-- Application theme. --> 
<style name="AppTheme" parent="AppBaseTheme"> 

    <!-- All customizations that are NOT specific to a particular API-level can go here. --> 
    <item name="android:popupMenuStyle">@style/MyPopupMenu</item> 
    <item name="android:itemTextAppearance">@style/MyCustomMenuTextAppearance</item> 

</style> 

<!-- Popup Menu Background Color styles --> 
<!-- <style name="MyPopupMenu" parent="@android:style/Widget.Holo.ListPopupWindow"> --> 
<!-- <style name="MyPopupMenu" parent="@android:style/Widget.PopupMenu"> --> 
<style name="MyPopupMenu" parent="@style/Widget.AppCompat.PopupMenu"> 
    <item name="android:popupBackground">@color/dark_gray</item> 
</style> 
<!-- Popup Menu Text Color styles --> 
<style name="MyCustomMenuTextAppearance"> 
    <item name="android:textColor">@color/white</item> 
</style> 

我一直堅持這個幾個小時,沒有類似的問題對我的解決方案。

如果有幫助,這裏是創建上下文菜單我的Java代碼:

@Override 
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { 
    super.onCreateContextMenu(menu, v, menuInfo); 

    AdapterView.AdapterContextMenuInfo info = 
      (AdapterView.AdapterContextMenuInfo) menuInfo; 
    String selectedWord = ((TextView) info.targetView).getText().toString(); 
    menu.setHeaderTitle(selectedWord); 

    MenuInflater inflater = getActivity().getMenuInflater(); 
    inflater.inflate(R.menu.shopping_list_name_context, menu); 
} 

而且,爲了完整起見,這裏是我的上下文菜單XML,shopping_list_name_context.xml

<item android:id="@+id/rename_shopping_list" 
     android:icon="@drawable/ic_action_edit" 
     android:title="@string/rename_shopping_list" /> 

<item android:id="@+id/empty_shopping_list" 
     android:icon="@drawable/ic_action_discard" 
     android:title="@string/empty_shopping_list" /> 

<item android:id="@+id/delete_shopping_list" 
     android:icon="@drawable/ic_action_discard" 
     android:title="@string/delete_shopping_list" /> 

而且,作爲r equested,這裏是我的AndroidManifest.xml的摘錄:

<uses-sdk 
    android:minSdkVersion="14" 
    android:targetSdkVersion="21" /> 

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
<uses-permission android:name="android.permission.USE_CREDENTIALS" /> 
<uses-permission android:name="android.permission.WAKE_LOCK" /> 
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 

<supports-screens  
    android:smallScreens="true" 
    android:normalScreens="true" 
    android:largeScreens="true" 
    android:xlargeScreens="true" /> 

<permission 
    android:name="com.example.myapp.permission.C2D_MESSAGE" 
    android:protectionLevel="signature" /> 

<uses-permission android:name="com.example.myapp.permission.C2D_MESSAGE" /> 
<uses-permission android:name="com.android.vending.BILLING" /> 

<application 
    android:name=".MyApplication" 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name_short" 
    android:theme="@style/AppTheme" 
    android:largeHeap="true" > 
+0

請問您可以發佈您的AppManifest.xml嗎? –

+0

好的,現在已經添加了它的相關部分。 –

+0

檢查此stackoverflow [發佈](http://stackoverflow.com/questions/30526667/appcompat-toolbar-popuptheme-not-used-when-multi-selection-active/30633124#30633124)。 – Elltz

回答

0

你需要覆蓋actionModeBackground這樣的:

<item name="android:actionModeBackground">@drawable/context_menu</item> 

可能重複的問題:Override context menu colors in Android

+0

我已經添加到我的''元素(並創建了** drawable-nodpi/context_menu.xml **),但恐怕它沒有沒有工作。 –

2

下面是代碼在上下文菜單中設置背景,它的作用就像魅力。

在styles.xml文件把下面的代碼:

<!-- Application theme. --> 
<style name="AppTheme" parent="AppBaseTheme"> 

    <item name="android:itemBackground">@android:color/holo_green_dark</item> 

</style> 
+0

感謝您的這一點,但它只會改變溢出菜單顏色 - 而不是上下文菜單顏色。 –

+0

嗨,兄弟,你只把一行itemBackground的風格,我必須在我的答案更新和刪除所有其他行,它的作品就像一個魅力,我已經測試 –

+0

你能告訴我,當你已經更新了你的代碼回答,我會再試一次。 –

0

嘗試添加此:

<item name="android:popupBackground">@android:color/white</item> 

我希望它能幫助!

+0

乾杯,但我得到這個錯誤的第二行:'錯誤:錯誤:找不到匹配給定名稱的資源:attr'popupBackground'.' –

+0

什麼是您的taargetSdkVersion? – Rajesh

+0

Target sdk版本是14. –

0

在您的應用程序欄XML:

app:popupTheme="@style/MyCustomOverflowTheme" 

然後在您的styles.xml:

<style name="MyCustomOverflowTheme" parent="Theme.AppCompat.Light"> 
    <item name="android:textColorPrimary">@color/primaryColor</item> 
    <item name="android:textColorSecondary">@color/accentColor</item> 
    <item name="android:background">@color/accentColor</item> 
</style> 

編輯

app:popupTheme="@style/MyCustomOverflowTheme" 

應該去爲你的工具欄或動作條財產,例如:

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/primaryColor" 
    app:theme="@style/MyCustomToolBarTheme" 
    app:popupTheme="@style/MyCustomOverflowTheme" 
    > 
</android.support.v7.widget.Toolbar> 
+0

感謝您的支持。你可以澄清'app:popupTheme =「@ style/MyCustomOverflowTheme」'行應該去哪裏? –

+0

如果minSdkVersion爲14,這個工作會嗎? –

+0

只注意到你的編輯。上下文菜單不是從工具欄或ActionBar中顯示,而是通過長按一個自定義TextView。我已經嘗試在xml中向自定義TextView添加'xmlns:app =「http://schemas.android.com/apk/res-auto」'和'app:popupTheme =「@ style/MyCustomOverflowTheme」',但是我只是得到'錯誤:找不到包'com.example.myapp'中屬性'popupTheme'的資源標識符。 –

0

您是否嘗試在您的ActionBar主題中放置「android:actionModeBackground」?

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar"> 
    <!-- main theme --> 
    ..... 
    <item name="actionBarStyle">@style/AppTheme.ActionBar</item> 

    <!-- if you're using Toolbar you have to put this --> 
    <item name="windowActionModeOverlay">true</item> 

</style> 

<style name="AppTheme.ActionBar" parent="Theme.AppCompat.Light"> 
     ..... 
    <item name="android:actionModeBackground">@color/theme_ambiance_dark</item> 
     ..... 
</style> 
+0

沒有工作,但無論如何感謝。 –