Sherlock Actionbar change menu background color

2019-08-15 14:20发布

问题:

I need to change the background of menu items only. When I tried with actionBarItemBackground, it changes the background of application logo [As u can see in the attachment] also. Any help or link will be appreciated.

回答1:

You can use a custom layout for your actionbar item like so:

    <menu xmlns:android="http://schemas.android.com/apk/res/android" >
         <item android:id="@id/item_text"
            android:showAsAction="always"
            android:actionLayout="@layout/action_text"/>
    </menu>

This is styleable as you wish. Another possibility would be adding only the ID (item_text in the example) and set the background like so:

 @Override
 public boolean onPrepareOptionsMenu(Menu menu) {
      MenuItem item = menu.findItem(R.id.item_text);
      item.getActionView().setBackgroundDrawable(new ColorDrawable(/* your color */));

      return super.onPrepareOptionsMenu(menu);
 }