changing title text color and size of menu android

2019-05-08 22:49发布

I am developing android application and in my application I am using Sherlock action bar. I am using some menu into my action bar.

<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@+id/card_menu"
      android:title="C123" 
      android:showAsAction="always">
    <menu>
        <item android:id="@+id/C0"
              android:title="C0"/>
        <item android:id="@+id/c1"
              android:title="c1" />
        <item android:id="@+id/c2"
              android:title="c2" />
    </menu>
    </item>
<item android:id="@+id/notification"
  android:title="Notifications"
  android:showAsAction="always"
  android:actionLayout="@layout/notification_icon"
  android:icon="@drawable/notification"
  />
<item android:id="@+id/filter"
  android:icon="@drawable/filter"
  android:title="Filter" 
  android:showAsAction="always"
  />

</menu>

and I display this menu into my activity like this..

inflater.inflate(R.menu.menu_type2, menu);

now what I want to do is I want to change my title color of my menu item (c123). I am able to change color of sub menu from style but not able to change of that item text. I tried this in following way.

// this change my sunbmenu text color but I am not able to change color of item text(c123)
<item name="android:dropDownListViewStyle">@style/MyPopupMenu</item>
<item name="dropDownListViewStyle">@style/MyPopupMenu</item>

<style name="MyPopupMenu" parent="Widget.Sherlock.ListPopupWindow">
 <item name="android:textAppearance">@style/TextAppearance.Sherlock.Widget.PopupMenu</item>
 <item name="android:dropDownSelector">@color/white</item>
 <item name="android:dividerHeight">@dimen/divider_height</item>
 <item name="android:divider">@color/maroon</item>

</style>

<style name="TextAppearance.Sherlock.Widget.PopupMenu" parent="android:style/TextAppearance.Holo.Widget.ActionBar.Menu">
 <item name="android:textColor">@color/white</item>
</style>  

I want to modify that menu item style change color and size of text and also background of that. How to do that. Need help.. Thank you..

1条回答
男人必须洒脱
2楼-- · 2019-05-08 23:31

You can change the color of the MenuItem text easily by using SpannableString instead of String.

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.your_menu, menu);

    int positionOfMenuItem = 0; // or whatever...
    MenuItem item = menu.getItem(positionOfMenuItem);
    SpannableString s = new SpannableString("My red MenuItem");
    s.setSpan(new ForegroundColorSpan(Color.RED), 0, s.length(), 0);
    item.setTitle(s);
}
查看更多
登录 后发表回答