How to change individual action item text color in ActionBar PROGRAMMATICALLY?

故事扮演 提交于 2019-12-11 01:03:59

问题


In my ActionBar, I have a MenuItem that has attribute showAsAction="always" as seen in the image below. Based on the connection a user has to our servers, I will be changing the text as well as color of the item.

Currently, I am able to change the text of the item very easily in onPrepareOptionsMenu(...):

 @Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem item = menu.findItem(R.id.action_connection);
    if(mIsConnected) {
        item.setTitle(R.string.action_connected);
    } else {
        item.setTitle(R.string.action_not_connected);
    }
    return super.onPrepareOptionsMenu(menu);
}

This works great and if possible, I would like to change the color of the text here as well. I've seen many posts about how to change the text of ALL the overflow items or the title to the ActionBar itself but nothing about changing an individual action item PROGRAMMATICALLY. The current color is set in xml, I want to change it dynamically.


回答1:


Well, each MenuItem View is actually a subclass of TextView, so this will make changing the text color easier.

A simple method you can use to locate a MenuItem View is View.findViewsWithText.

A basic implementation, considering you just have that one MenuItem you're interested in changing, might look something like this:

private final ArrayList<View> mMenuItems = Lists.newArrayList();
private boolean mIsConnected;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Add a your MenuItem
    menu.add("Connected").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    // Adjust the text color based on the connection
    final TextView connected = !mMenuItems.isEmpty() ? (TextView) mMenuItems.get(0) : null;
    if (connected != null) {
        connected.setTextColor(mIsConnected ? Color.GREEN : Color.RED);
    } else {
        // Find the "Connected" MenuItem View
        final View decor = getWindow().getDecorView();
        decor.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                mIsConnected = true;
                // Remove the previously installed OnGlobalLayoutListener
                decor.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                // Traverse the decor hierarchy to locate the MenuItem
                decor.findViewsWithText(mMenuItems, "Connected",
                        View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
                // Invalidate the options menu to display the new text color
                invalidateOptionsMenu();
            }

        });

    }
    return true;
}

Results



来源:https://stackoverflow.com/questions/28543760/how-to-change-individual-action-item-text-color-in-actionbar-programmatically

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