如何改变个人的行动项文本的颜色在动作条编程? [英] How to change individual action item text color in ActionBar PROGRAMMATICALLY?
问题描述
在我的动作条
,我有一个菜单项
有属性 showAsAction =总是
如下图所示。基于连接上的用户到我们的服务器,我将更改文本,以及该项目的颜色。
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.
目前,我能够很容易地改变该项目的文本在prepareOptionsMenu(...)
:
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);
}
这伟大工程,如果可能的话,我想改变文本的颜色在这里。我已经看到了有关如何更改所有溢出的项目要改变一个人的行动项目编程的文字或标题为动作条
本身,但没有很多帖子。目前的颜色是XML格式设置,我要动态地改变它。
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.
推荐答案
好了,每个菜单项
查看
实际上是<一href="https://github.com/android/platform_frameworks_base/blob/59701b9ba5c453e327bc0e6873a9f6ff87a10391/core/java/com/android/internal/view/menu/ActionMenuItemView.java"相对=nofollow> 的TextView
的一个子类,所以这将使改变文字颜色更容易。
Well, each MenuItem
View
is actually a subclass of TextView
, so this will make changing the text color easier.
有一个简单的方法,你可以用它来找到菜单项
查看
是<一个href="https://developer.android.com/reference/android/view/View.html#findViewsWithText(java.util.ArrayList%3Candroid.view.View%3E,%20java.lang.CharSequence,%20int)"相对=nofollow> View.findViewsWithText
。
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;
}
结果
这篇关于如何改变个人的行动项文本的颜色在动作条编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!