如何在不设置工具栏样式的情况下动态更改所有工具栏图标的颜色

Mig*_*ieC 4 android android-appcompat android-imagebutton android-toolbar

我一直在寻找一种方法来动态更改工具栏(如ActionBar)中所有元素的颜色。

规格:

  • parent="Theme.AppCompat.Light.NoActionBar"在styles.xml上使用
  • Appcompat v7 22
  • 设置setSupportActionBar()在我的AppCompatActivity
  • 我从POST请求中获得了颜色(通常是#FF ------格式)

我已阅读以下帖子:

  1. 如何更改ActionBar汉堡包图标的颜色?
  2. 如何在材料设计导航抽屉中更改汉堡图标的颜色
  3. 无法在Android中更改导航抽屉图标的颜色
  4. ActionBarDrawerToggle v7箭头颜色
  5. Android工具栏颜色更改
  6. Android burger / arrow图标动态更改颜色(此方法某种程度上可行,但我不喜欢使用自己的图像动画)。
    以及与此主题相关的其他链接...没有一个对我有用。

我现在正在做的是在工具栏上搜索ImageButton(在support actionbar中获取对抽屉切换的引用),然后setColorFilter()像下面的代码一样将它们应用于所有对象:

for (int i = 0; i < toolbar.getChildCount(); i++){
    if (toolbar.getChildAt(i) instanceof ImageButton) {
        ImageButton ib = (ImageButton) toolbar.getChildAt(i);
        ib.setColorFilter(Color.parseColor("#A74231"), PorterDuff.Mode.SRC_ATOP);
    }
}
Run Code Online (Sandbox Code Playgroud)

我改变的背景和文本颜色搭配:toolbar.setBackgroundColortoolbar.setTitleTextColor

对于菜单图标(包括溢出菜单图标):

MenuItem item2 = mMenu.findItem(R.id.actionbar_group_moreoverflow);
item2.getIcon().setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
Run Code Online (Sandbox Code Playgroud)


问题:有没有更好的办法做到这一点(改变工具栏的元素色彩动态)?

小智 5

我在这里面临着同样的问题。我为ToolBar的元素做了什么:

  1. 对于背景色: toolbar.setBackgroundColor(Color.parseColor("#xxxxxxxx"));
  2. 对于文本颜色: toolbar.setTitleTextColor(Color.parseColor("#xxxxxxxx"));
  3. 对于汉堡包/抽屉按钮:

    int color = Color.parseColor("#xxxxxxxx");
    final PorterDuffColorFilter colorFilter = new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP);
    
    for (int i = 0; i < toolbar.getChildCount(); i++){
    
        final View v = toolbar.getChildAt(i);
    
        if(v instanceof ImageButton) {
            ((ImageButton)v).setColorFilter(colorFilter);
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  4. 对于ActionMenuItemView(工具栏的按钮,包括溢出按钮):

    private void colorizeToolBarItem(AppCompatActivity activity, final PorterDuffColorFilter colorFilter, final String description) {
    
        final ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
        final ViewTreeObserver viewTreeObserver = decorView.getViewTreeObserver();
    
        viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
    
                final ArrayList<View> outViews = new ArrayList<>();
                decorView.findViewsWithText(outViews, description,
                    View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
                if (outViews.isEmpty())
                    return;
    
                ActionMenuItemView overflow = (ActionMenuItemView)outViews.get(0);
                overflow.getCompoundDrawables()[0].setColorFilter(colorFilter);
                removeOnGlobalLayoutListener(decorView,this);
            }
        });
    }
    
    Run Code Online (Sandbox Code Playgroud)
  5. 对于溢出菜单的项目文本:请看此链接


归档时间:

查看次数:

3825 次

最近记录:

10 年,2 月 前