【问题标题】:android:popupBackground not working for context menusandroid:popupBackground 不适用于上下文菜单
【发布时间】:2019-07-19 12:36:24
【问题描述】:

为什么我的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" >

【问题讨论】:

  • 你能发布你的 AppManifest.xml 吗?
  • 好的,现在已经添加了它的相关部分。
  • 检查这个stackoverflow Post
  • 我不得不求助于更改前景色,但从那以后我有了很多新的答案,所以我现在就去看看......
  • @Heyyou 我已经尝试了所有建议,但没有一个奏效。我认为一些回答者认为这是我要更改的溢出菜单,而实际上我是在尝试更改长按视图后显示的上下文菜单(在本例中为自定义文本视图)。

标签: android xml android-contextmenu


【解决方案1】:

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

在styles.xml 文件中放入以下代码:

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

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

</style>

【讨论】:

  • 感谢您,但它只会更改溢出菜单背景颜色 - 而不是上下文菜单背景颜色。
  • 嘿兄弟,你只放了一个单行 itemBackground 样式,我必须在我的答案中更新并删除所有其他行,它就像一个魅力,我已经测试过了
  • 你能告诉我你什么时候更新了答案中的代码,我会再试一次。
  • 如上所示,我在应用程序样式中添加了 android:itemBackground。只需输入一行并检查它。我在我的应用程序中测试过它可以工作。
【解决方案2】:

您需要像这样覆盖 actionModeBackground:

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

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

【讨论】:

  • 我已将其添加到我的 &lt;style name="AppTheme" parent="AppBaseTheme"&gt;...&lt;/style&gt; 元素中(并创建了 drawable-nodpi/context_menu.xml),但恐怕它没有用。跨度>
【解决方案3】:

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

应该作为您的 Toolbar 或 ActionBar 属性,例如:

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

【讨论】:

  • 谢谢。你能澄清app:popupTheme="@style/MyCustomOverflowTheme" 行应该去哪里吗?
  • 刚刚注意到您的编辑。上下文菜单不是从 Toolbar 或 ActionBar 显示的,而是通过长按自定义 TextView 来显示的。我尝试将xmlns:app="http://schemas.android.com/apk/res-auto"app:popupTheme="@style/MyCustomOverflowTheme" 添加到xml 中的自定义TextView,但我只得到error: No resource identifier found for attribute 'popupTheme' in package 'com.example.myapp'
  • 出现错误,因为 TextView 无法使用此属性。
  • 谢谢,是的。结果是它不是我的解决方案。
【解决方案4】:

您是否尝试将“android:actionModeBackground”放入您的 ActionBar 主题中?

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

    尝试添加这个:

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

    希望对你有帮助!

    【讨论】:

    • 干杯,但第二行出现此错误:error: Error: No resource found that matches the given name: attr 'popupBackground'.
    • 你的 taargetSdkVersion 是什么?
    • 目标sdk版本为14。
    • 删除这一行:​​@android:color/white
    【解决方案6】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多