以编程方式更改MenuItem文本颜色 [英] Change MenuItem text color programmatically

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

问题描述

所以我有一个菜单项,其定义为:

So I have a menu item, that's defined as:

<item
    android:id="@+id/action_live"
    android:title="@string/action_live"
    android:orderInCategory="1"
    app:showAsAction="ifRoom|withText" />

它显示为文本,如下所示:

It shows as text, as you can see below:

我想以编程方式更改实时"文本的颜色.我搜索了一段时间,发现了一种方法:

And I want to programmatically change the "LIVE" text color. I've searched for a while and I found a method:

具有全局定义:

private Menu mOptionsMenu;

和:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    mOptionsMenu = menu;
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

我这样做:

MenuItem liveitem = mOptionsMenu.findItem(R.id.action_live);
SpannableString s = new SpannableString(liveitem.getTitle().toString());
s.setSpan(new ForegroundColorSpan(Color.RED), 0, s.length(), 0);
liveitem.setTitle(s);

但是什么也没发生!

如果我对溢出菜单的某个项目执行相同的操作,则它会起作用:

If I do the same for an item of the overflow menu, it works:

app:showAsAction ="ifRoom | withText"项目是否有某些限制?有什么解决方法吗?

Is there some limitation for app:showAsAction="ifRoom|withText" items? Is there any workaround?

谢谢.

推荐答案

跟这个聚会有点晚了,但是我花了一段时间研究这个问题,发现了一个解决方案,该解决方案可能对其他尝试这样做的人有用同样的事情.值得称赞的是Harish Sridharan,他为我指引了正确的方向.

Bit late to the party with this one, but I spent a while working on this and found a solution, which may be of use to anyone else trying to do the same thing. Some credit goes to Harish Sridharan for steering me in the right direction.

您可以使用findViewById(R.id.MY_MENU_ITEM_ID)定位菜单项(前提是已创建并准备好菜单),然后将其投射到Harish建议的TextView实例中,然后可以根据需要对其进行样式设置.

You can use findViewById(R.id.MY_MENU_ITEM_ID) to locate the menu item (provided that the menu had been created and prepared), and cast it to a TextView instance as suggested by Harish, which can then be styled as required.

public class MyAwesomeActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    super.onCreate(savedInstanceState);

    // Force invalidatation of the menu to cause onPrepareOptionMenu to be called
    invalidateOptionsMenu();
}

private void styleMenuButton() {
    // Find the menu item you want to style
    View view = findViewById(R.id.YOUR_MENU_ITEM_ID_HERE);

    // Cast to a TextView instance if the menu item was found
    if (view != null && view instanceof TextView) {
        ((TextView) view).setTextColor( Color.BLUE ); // Make text colour blue
        ((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_SP, 24); // Increase font size
    }
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    boolean result = super.onPrepareOptionsMenu(menu);
    styleMenuButton();
    return result;
}

}

此处的技巧是强制菜单在活动的onCreate事件中失效(从而导致onPrepareMenuOptions的调用比通常的调用更快).在此方法内,我们可以根据需要找到菜单项和样式.

The trick here is to force the menu to be invalidated in the activity's onCreate event (thereby causing the onPrepareMenuOptions to be called sooner than it would normally). Inside this method, we can locate the menu item and style as required.

这篇关于以编程方式更改MenuItem文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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