Android MenuItem 设置文本颜色-TextColor设置

it2025-08-01  3

前面一直在寻找 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的颜色成功改变。

<!------ 补充  这是近期发现的。

上面的 有些机型不能使用----------------> 看到:http://stackoverflow.com/questions/18015010/action-bar-menu-item-text-color        stackOverFlow有相关解答。

版权声明:本文博客原创文章。博客,未经同意,不得转载。

转载于:https://www.cnblogs.com/bhlsheji/p/4679170.html

最新回复(0)