简体   繁体   中英

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

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.

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

结果

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM