我在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
的context Menu
项目或整个上下文菜单指定背景颜色。有没有办法单独设置它?我可以在不影响option Menu
的情况下另外设置context Menu
项目的背景颜色吗?
遗憾的是你不能自定义上下文菜单,但你可以创建一个Dialog
并将其用作ContextMenu
.something,如下所示:
@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>
设置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>
如果您只想影响菜单的下拉菜单,可以设置android:dropDownListViewStyle
。这将处理上下文菜单以及操作栏溢出菜单中的下拉菜单。我没有调查这是否也会影响PopupMenu
s。
<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
。