我已经看到有关如何更改操作栏溢出菜单的所有菜单项的背景颜色的各种问题,但是我的情况是我需要为特定的菜单项设置自定义背景在侧面导航抽屉中。
这是我目前所拥有的(菜单项需要将背景手动突出显示为红色):
如您所见,我已经将相关菜单项的文本颜色更改为橙色。我必须以编程方式执行此操作-像这样:
MenuItem menuItem = ...;
int colorOrange = ...;
SpannableString s = new SpannableString(menuItem.getTitle());
s.setSpan(new ForegroundColorSpan(colorOrange), 0, s.length(), 0);
menuItem.setTitle(s);
但是如何更改这些菜单项的背景颜色?
为完整起见,这是我的 menu_nav_drawer.xml 的相应部分:
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group
android:checkableBehavior="single">
...
<item
android:id="@+id/menu_item_why_upgrade"
android:icon="@drawable/ic_thumb_up_white_24dp"
android:title="@string/why_upgrade_qm" />
<item
android:title="@string/latest_and_local">
<menu>
<group android:checkableBehavior="single">
<item
android:id="@+id/menu_item_gmo_news"
android:icon="@drawable/ic_announcement_white_24dp"
android:title="@string/latest_gmo_news" />
<item
android:id="@+id/menu_item_local_gmo_free"
android:icon="@drawable/ic_place_white_24dp"
android:title="@string/local_gmo_free" />
</group>
</menu>
</item>
<item
android:title="@string/miscellaneous">
<menu>
<group android:checkableBehavior="single">
<item
android:id="@+id/menu_item_settings"
android:icon="@drawable/ic_settings_white_24dp"
android:title="@string/settings" />
<item
android:id="@+id/menu_item_share"
android:icon="@drawable/ic_share_white_24dp"
android:title="@string/share_this_app" />
<item
android:id="@+id/menu_item_leave_review"
android:icon="@drawable/ic_star_white_24dp"
android:title="@string/leave_rating_feedback_tc" />
<item
android:id="@+id/menu_item_the_holistic_works_website"
android:icon="@drawable/ic_public_white_24dp"
android:title="@string/the_holistic_works" />
</group>
</menu>
</item>
<item
android:title="@string/legals">
<menu>
<group android:checkableBehavior="single">
<item
android:id="@+id/menu_item_privacy_policy"
android:icon="@drawable/ic_info_outline_white_24dp"
android:title="@string/privacy_policy" />
<item
android:id="@+id/menu_item_terms_and_conditions"
android:icon="@drawable/ic_info_outline_white_24dp"
android:title="@string/terms_and_conditions" />
<item
android:id="@+id/menu_item_disclaimer_and_copyright"
android:icon="@drawable/baseline_copyright_white_24"
android:title="@string/disclaimer_and_copyright" />
</group>
</menu>
</item>
</group>
</menu>