Android MenuItem 设置文字颜色-TextColor的设置
前面一直在找 MenuItem的文字颜色的设置。我发现API中只有背景颜色的设置。。。所以找到下面的方法。在OverFlow上看到的。
在onCreateOptionsMenu中覆写一下。使MenuItem产生的ItemView去修改文字颜色
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
getLayoutInflater().setFactory(new Factory()
{
@Override
public View onCreateView(String name, Context context,
AttributeSet attrs)
{
System.out.println(name);
if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")
|| name.equalsIgnoreCase("com.android.internal.view.menu.ActionMenuItemView"))
{
try
{
LayoutInflater f = getLayoutInflater();
final View view = f.createView(name, null, attrs);
System.out.println((view instanceof TextView));
if(view instanceof TextView){
((TextView)view).setTextColor(Color.GREEN);
}
return view;
} catch (InflateException e)
{
e.printStackTrace();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
return null;
}
});
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
可以看到MenuItem的颜色成功改变。