我在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
也采用相同的颜色。
我的问题是我如何为Menu
的{{1}}项或整个上下文菜单指定背景颜色。有没有办法单独设置它?我是否可以在不影响context Menu
的情况下另外设置option Menu
项目的背景颜色?
答案 0 :(得分: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>
答案 1 :(得分:0)
设置背景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>
答案 2 :(得分:0)
如果只想影响菜单的下拉菜单,则可以设置android:dropDownListViewStyle
。这将处理上下文菜单以及操作栏的溢出菜单中的下拉菜单。我尚未调查这是否还会影响PopupMenu
。
<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:colorBackground
上查看Android's Documentation。