android change option menu text color

别等时光非礼了梦想. 提交于 2020-01-04 09:27:12

问题


I have been developing app these days, the code for changing text items:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater=getMenuInflater();
    inflater.inflate(R.menu.main, 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.setTitleCondensed(s);
    return true;
}

it works pretty, but when I clicking the menu item it stops with error


回答1:


Use one of the Spannable flag constants as the last argument to the call to setSpan(), e.g. Spannable.SPAN_EXCLUSIVE_EXCLUSIVE. See http://developer.android.com/reference/android/text/Spannable.html.

Also, consider moving the code after inflate to onPrepareOptionsMenu().

The following post might be useful: http://www.techrepublic.com/article/style-androids-overflow-menu-using-this-sanity-saving-workaround/



来源:https://stackoverflow.com/questions/21898745/android-change-option-menu-text-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!