动态更改工具栏的文本颜色MenuItem文本
在应用程序中,我正在处理来自HTTP响应的各种UI元素的颜色,即着色在运行时完成,无法使用Android主题完成。动态更改工具栏的文本颜色MenuItem文本
有关此主题的其他问题,但大多数都使用主题,或者如果动态代码,代码不起作用。例如,使用跨度:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menuItemGoalSave = menu.findItem(R.id.action_goal_edit_save);
SpannableString s = new SpannableString(menuItemGoalSave.getTitle() + "XX");
ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
s.setSpan(span, 0, s.length(), 0);
menuItemGoalSave.setTitle(s);
return true;
}
或应用HTML字符串的类似方法。
理论上面的代码使用着色的跨度,但虽然标题附加了“XX”(即代码正在运行并影响菜单项),但颜色不会改变。
有什么办法可以动态地影响这个工具栏动作项目文本的颜色吗?
试试这个:
TextView liveitem = (TextView)mOptionsMenu.findItem(R.id.action_live);
liveitem.setTextColor(Color.RED);
我使用的是支持库,不能投的菜单项来TextView的(或AppCompatTextView)至少在onPrepareOptionsMenu():java.lang.ClassCastException:android.support.v7.view.menu.MenuItemImpl不能转换为android.widget.TextView或者也许转换可能在Activity生命周期的不同点? –
更新
而且它的工作原理是在预期的情况下,如果你使用 '工具栏' 直接。见例如
public class MainActivity extends AppCompatActivity implements Toolbar.OnMenuItemClickListener {
private Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
mToolbar.inflateMenu(R.menu.menu_main);
mToolbar.setOnMenuItemClickListener(this);
}
private void applyMenuItemTextColor() {
MenuItem item = mToolbar.getMenu().findItem(R.id.action_settings);
SpannableString s = new SpannableString(item.getTitle());
ForegroundColorSpan span = new ForegroundColorSpan(System.currentTimeMillis() % 2 == 0 ? Color.RED :Color.GREEN);
s.setSpan(span, 0, s.length(), 0);
item.setTitle(s);
}
@Override
public boolean onMenuItemClick(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
applyMenuItemTextColor();//supportInvalidateOptionsMenu();
return true;
}
return super.onOptionsItemSelected(item);
}
}
原来的答复
你的跨度例如按预期工作。看到的结果:
下面是用于此示例代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.action_settings);
SpannableString s = new SpannableString(item.getTitle());
ForegroundColorSpan span = new ForegroundColorSpan(System.currentTimeMillis() % 2 == 0 ? Color.RED :Color.GREEN);
s.setSpan(span, 0, s.length(), 0);
item.setTitle(s);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
supportInvalidateOptionsMenu();
return true;
}
return super.onOptionsItemSelected(item);
}
}
我希望这将是有益的。如果有任何问题,请告诉我。
我仔细地将我的代码与您的代码逐行比较,并且据我所见,它们是相同的。我在原始问题中扩展了代码示例,并将其从我的源代码中粘贴。我已经尝试了许多代码变体,但尚未看到它的工作。我已经在N(模拟),棒棒糖和Marshamallow上测试过它。我的活动也从AppCompatActivity继承。显然我错过了一些东西,但我不知道是什么。 –
请在什么时刻澄清你设置新的颜色 –
我有灵活性,但目前正在试图做到这一点在onPrepareOptionsMenu() –