安卓 - 更改MaterialToolbar菜单项图标颜色
创始人
2024-09-01 14:30:25
0

要更改MaterialToolbar菜单项图标的颜色,可以通过设置Toolbar的Menu资源,并使用适当的颜色过滤器来更改图标的颜色。以下是一个示例代码:

  1. 在布局文件中,将Toolbar添加到你的布局中:

  1. 在你的Activity或Fragment中,找到Toolbar并设置菜单资源:
MaterialToolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.inflateMenu(R.menu.menu_main);
  1. 创建一个自定义方法来更改图标颜色:
private void changeMenuIconColor(Menu menu, @ColorInt int color) {
    for (int i = 0; i < menu.size(); i++) {
        MenuItem item = menu.getItem(i);
        Drawable drawable = item.getIcon();
        if (drawable != null) {
            drawable.mutate();
            drawable.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP));
        }
    }
}
  1. 在onCreateOptionsMenu方法中调用changeMenuIconColor方法来更改图标颜色:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    changeMenuIconColor(menu, Color.WHITE); // 设置图标颜色为白色
    return true;
}

这样,你就可以通过调用changeMenuIconColor方法并传入所需的颜色来更改MaterialToolbar菜单项图标的颜色了。

相关内容

热门资讯

第4分钟辅助!wepoker透... 第4分钟辅助!wepoker透视方法,德州私人局脚本,举措教程(有挂头条)德州私人局脚本能透视中分为...
9分钟辅助!德普之星私人局辅助... 9分钟辅助!德普之星私人局辅助器,xpoker辅助神器,机巧教程(证实有挂)进入游戏-大厅左侧-新手...
第3分钟辅助!we poker... 第3分钟辅助!we poker辅助器v3.3,wepoker透视功能下载,机巧教程(有挂透视)1、超...
第8分钟辅助!wepoker怎... 第8分钟辅助!wepoker怎么开辅助,黑侠破解wepoker,课程教程(今日头条)1、上手简单,内...
第十分钟辅助!如何下载wpk透... 第十分钟辅助!如何下载wpk透视版,pokerworld软件,策略教程(有挂分享)如何下载wpk透视...
九分钟辅助!wepoker辅助... 九分钟辅助!wepoker辅助真的假的,cloudpoker作弊,手筋教程(证实有挂)1、每一步都需...
第十分钟辅助!wejoker辅... 第十分钟辅助!wejoker辅助器怎么卖,wepoker免费透视脚本,窍门教程(有挂方式)1、进入游...
十分钟辅助!wepoker-h... 十分钟辅助!wepoker-h5下载,pokernow辅助控制,绝活教程(有挂实锤)1、点击下载安装...
5分钟辅助!aapoker辅助... 5分钟辅助!aapoker辅助器怎么用,wepoker模拟器哪个好用,模板教程(有挂辅助)wepok...
八分钟辅助!wpk官网下载链接... 八分钟辅助!wpk官网下载链接,wpk透视怎么安装,教程书教程(有挂分享)该软件可以轻松地帮助玩家将...