Android(安卓)MenuItem 设置文字颜色-TextColor的设置
16lz
2021-01-26
前面一直在找 MenuItem的文字颜色的设置。我发现API中只有背景颜色的设置。。。所以找到下面的方法。在OverFlow上看到的。
在onCreateOptionsMenu中覆写一下。使MenuItem产生的ItemView去修改文字颜色
@Overridepublic 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上有相关的答案。
更多相关文章
- android学习笔记23:幻灯片
- 自动化测试 Appium之Python运行环境搭建 Part2
- Android中Listview通过适配器设置Item的高度及其他样式
- Android使用饺子播放器播放服务器端的视频
- vapor开发随笔
- Android中ListView中使用CheckedTextView
- 解决Android创建AVD失败
- Android中一些实用的Tips
- Android(安卓)中状态栏(屏幕顶部)消息的显示 Notification