android:popupBackground不适用于上下文菜单

时间:2015-08-20 22:12:05

标签: android xml android-contextmenu

为什么我的styles.xml代码成功更改了操作栏溢出菜单的背景颜色,但无法更改应用中上下文菜单的背景颜色?

<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>

我已经坚持了几个小时,因此类似问题的SO解决方案都没有对我有用。

如果有帮助,这是我的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" />

并且,根据要求,这是我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" >

6 个答案:

答案 0 :(得分:1)

下面是上下文菜单中设置背景的代码,它就像魅力一样。

在styles.xml文件中放置代码:

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

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

</style>

答案 1 :(得分:0)

你需要像这样覆盖actionModeBackground:

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

可能重复的问题:Override context menu colors in Android

答案 2 :(得分:0)

尝试添加此内容:

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

我希望它有所帮助!

答案 3 :(得分:0)

在您的应用栏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>

然后在你的styles.xml中:

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>

应该作为您的工具栏或ActionBar属性,例如:

function generateJwt(){
    var deferred = Q.defer();
deferred.resolve({
            message: 'user created',
            token: signedJwt,
            userId: user.userId
          });
deferred.promise.then(function success(result) {
    console.log(result)
  }, function failed(result) {
    console.log(result)
  });
  return deferred.promise;

 }

答案 4 :(得分: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>

答案 5 :(得分:0)

我通过使用“ contextPopupMenuStyle”而不是“ popupMenuStyle”使它正常工作。