【问题标题】:How to specify the background color of the context Menu如何指定上下文菜单的背景颜色
【发布时间】:2016-12-16 18:56:31
【问题描述】:

我在一个 android 应用程序中使用上下文菜单来提示用户选择一个过滤器。相关代码为:

TextView someView = (TextView) findViewById(id.some_view);
registerForContextMenu(someView);

...

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
{
     super.onCreateContextMenu(menu, v, menuInfo);
     menu.setHeaderTitle("Select a Filter");

     menu.add(0, 0, 0, "Filter1");
     menu.add(0, 1, 1, "Filter2");
     menu.add(0, 2, 2, "Filter3");
}

@Override
public boolean onContextItemSelected(MenuItem item)
{
     if (item.getItemId() == 0)
     {
        ...
     }
     else
     {
        ...
     }
     return true;
}

在 styles.xml 文件中,我使用以下行:

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

设置option Menu 项目的背景颜色。我希望它们具有与Actionbar 相同的颜色。然而,效果是context Menu 也呈现相同的颜色。

我的问题是如何为 context MenuMenu 项目或整个上下文菜单指定背景颜色。有没有办法单独设置?我可以在不影响context Menu 的情况下以其他方式设置option Menu 项目的背景颜色吗?

【问题讨论】:

    标签: android menu styles


    【解决方案1】:

    不幸的是,您无法自定义上下文菜单,但您可以创建一个Dialog 并将其用作ContextMenu。像这样:

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
    {
        final Dialog dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.menu_layout);
    
        LinearLayout ll_menu=(LinearLayout)findViewById(R.id.ll_menu);
    
        //add some TextView or ImageView to ll_menu as menu items
    
    
        dialog.show();
    }
    

    menu_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_height="match_parent"
         android:layout_width="match_parent">
    
       <LinearLayout
         android:orientation="vertical"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:id="@+id/ll_menu"
         >
    
    
      </LinearLayout>
    
    </ScrollView>
    

    【讨论】:

      【解决方案2】:

      设置android:dropDownListViewStyle 的背景确实对我有用

      <resources>
      
          <!-- Base application theme. -->
          <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
              <!-- Customize your theme here. -->
              <item name="colorPrimary">@color/colorPrimary</item>
              <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
              <item name="colorAccent">@color/colorAccent</item>
              <item name="android:dropDownListViewStyle">@style/AppTheme.DropDownList</item>
          </style>
          <style name="AppTheme.DropDownList" parent="Widget.AppCompat.ListView.DropDown">
              <item name="android:background">#f00</item>
          </style>
      
      </resources>
      

      【讨论】:

        【解决方案3】:

        如果您只想影响菜单的下拉菜单,您可以设置android:dropDownListViewStyle。这将处理上下文菜单以及操作栏溢出菜单中的下拉菜单。我还没有调查这是否也会影响PopupMenus。

        <resources>
            <!-- Base application theme that you would set in your AndroidManifest. -->
            <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
                <!-- Theme customizations that will affect your whole application (or anywhere you apply the theme). -->
                <item name="android:dropDownListViewStyle">@style/AppTheme.DropDownList</item>
            </style>
        
            <style name="AppTheme.DropDownList" parent="Widget.AppCompat.ListView.DropDown">
                <item name="android:background">@android:color/background_light</item>
            </style>
        </resources>
        

        另一个选项是通过设置android:colorBackground 来设置应用程序主题的全屏窗口中使用的背景颜色,如下所示:

        <resources>
            <!-- Base application theme that you would set in your AndroidManifest. -->
            <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
                <!-- Theme customizations that will affect your whole application (or anywhere you apply the theme). -->
                <item name="android:colorBackground">@android:color/background_light</item>
            </style>
        </resources>
        

        查看Android's Documentation 了解有关android:colorBackground 的更多信息。

        【讨论】:

          猜你喜欢
          • 2020-08-14
          • 1970-01-01
          • 2012-01-04
          • 1970-01-01
          • 1970-01-01
          • 2020-09-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多