更改Android选项菜单文本颜色 [英] android change option menu text color
问题描述
我一直在开发应用程序,这些天,code更改文本项目:
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;
}
它的工作原理pretty,但是当我点击菜单项,其出现错误而终止
it works pretty, but when I clicking the menu item it stops with error
推荐答案
使用 Spannable
标志常量作为最后一个参数来调用之一 setSpan()
,例如: Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
。请参见 http://developer.android.com/reference/android/text/Spannable。 HTML 。
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.
此外,考虑在prepareOptionsMenu移动后的code膨胀
到()
。
Also, consider moving the code after inflate
to onPrepareOptionsMenu()
.
以下信息可能是有用的:<一href=\"http://www.techrepublic.com/article/style-androids-overflow-menu-using-this-sanity-saving-workaround/\" rel=\"nofollow\">http://www.techrepublic.com/article/style-androids-overflow-menu-using-this-sanity-saving-workaround/
The following post might be useful: http://www.techrepublic.com/article/style-androids-overflow-menu-using-this-sanity-saving-workaround/
这篇关于更改Android选项菜单文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!