问题
I use a context menu in an android App to prompt the user to select a Filter. The relevant code is:
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;
}
In the styles.xml file I use the line:
<item name="android:itemBackground">@color/someColor</item>
to set the background color of the option Menu
items. I would like them to have the same color as the Actionbar
. The effect is however that the context Menu
takes on also the same color.
My question is how I can specify a background color for the Menu
items of the context Menu
or for the whole context Menu. Is there a way to set it individually? Could I alternatively set the background Color of the option Menu
items on an other way without affecting the context Menu
?
回答1:
unfortunately You can't customize the context menu,but you can create a Dialog
and use that as a ContextMenu
.something like this :
@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:
Setting a background of android:dropDownListViewStyle
did work for me
<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:
If you only want to affect dropdowns for menus you can set android:dropDownListViewStyle
. This will handle context menus as well as the dropdown menu from the actionbar's overflow menu. I haven't looked into if this also affects 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>
Another option is to set the background color that is used in full screen windows across your application theme by setting android:colorBackground
like so:
<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>
Check out Android's Documentation for more on android:colorBackground
.
来源:https://stackoverflow.com/questions/38872585/how-to-specify-the-background-color-of-the-context-menu