更改Android选项菜单文本颜色 [英] android change option menu text color

查看:292
本文介绍了更改Android选项菜单文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在开发应用程序,这些天,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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆