如何动态地改变所有工具栏的图标颜色而不造型工具栏 [英] How to change all ToolBar's icons color dynamically without styling toolbar

查看:616
本文介绍了如何动态地改变所有工具栏的图标颜色而不造型工具栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一种方法来改变的所有在工具栏就像一个动作条的工作动态元素的颜色。

I have been looking a way to change the color of all elements in a toolbar working like an ActionBar dynamically.

规格:

  • 使用父=Theme.AppCompat.Light.NoActionBar在styles.xml
  • Appcompat V7 22
  • 制定 setSupportActionBar()在我的 AppCompatActivity
  • 在我从一个POST请求(通常#FF ------格式)
  • 的颜色
  • Using parent="Theme.AppCompat.Light.NoActionBar" on styles.xml
  • Appcompat v7 22
  • setting setSupportActionBar() in my AppCompatActivity
  • I got the colors from a POST request (usually #FF------ format)

我看了下面的帖子:

I have read following post:

  1. <一个href="http://stackoverflow.com/questions/30760807/how-do-i-change-the-color-of-the-actionbar-hamburger-icon">How修改我的动作条汉堡包图标的颜色?
  2. <一个href="http://stackoverflow.com/questions/31870132/how-to-change-color-of-hamburger-icon-in-material-design-navigation-drawer">How改变汉堡包图标的颜色材料设计抽屉式导航
  3. <一个href="http://stackoverflow.com/questions/31505950/cant-change-navigation-drawer-icon-color-in-android">Can't变化的抽屉式导航栏图标的颜色在android的
  4. <一个href="http://stackoverflow.com/questions/27861102/actionbardrawertoggle-v7-arrow-color">ActionBarDrawerToggle V7箭头颜色
  5. Android的工具栏颜色变化
  6. <一个href="http://stackoverflow.com/questions/32228199/android-burger-arrow-icon-dynamic-change-color">Android汉堡/箭头图标动态变化颜色(以某种方式这个工作,但我不喜欢使用自己的形象wihtout动画)。
    而其他人的联系与此相关的话题......他们都为我工作。

  1. How do I change the color of the ActionBar hamburger icon?
  2. How to change color of hamburger icon in material design navigation drawer
  3. Can't change navigation drawer icon color in android
  4. ActionBarDrawerToggle v7 arrow color
  5. Android Toolbar color change
  6. Android burger/arrow icon dynamic change color (this one worked in someway but I don't like using own image wihtout animation).
    And others links related to this topic... none of them worked for me.

我在做什么,现在正在寻找的ImageButton工具栏上(<一href="http://stackoverflow.com/questions/28279953/get-reference-to-drawer-toggle-in-support-actionbar">Get参考抽屉切换,支持动作条),并应用 setColorFilter()来所有的人都像下面的code:

What I'm doing right now is searching for ImageButton on the toolbar (Get reference to drawer toggle in support actionbar), and applying setColorFilter() to all of them like the following code:

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);
    }
}

我改变了背景和文本颜色: toolbar.setBackgroundColor toolbar.setTitleTextColor

有关菜单图标(包括溢出菜单图标):

For menu icons (including overflow menu icon):

MenuItem item2 = mMenu.findItem(R.id.actionbar_group_moreoverflow);
item2.getIcon().setColorFilter(color, PorterDuff.Mode.SRC_ATOP);



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

the QUESTION: is there a better way to do it (change toolbar's elements color dynamically)?

推荐答案

我在这里面临着同样的问题。我所做的工具栏上的元素:

I was facing same problem here. What I did for ToolBar's elements:

  1. 有关背景颜色: toolbar.setBackgroundColor(Color.parseColor(#XXXXXXXX));
  2. 对于文本颜色: toolbar.setTitleTextColor(Color.parseColor(#XXXXXXXX));
  3. 有关汉堡包/纸盒键:

  1. For background color: toolbar.setBackgroundColor(Color.parseColor("#xxxxxxxx"));
  2. For text color: toolbar.setTitleTextColor(Color.parseColor("#xxxxxxxx"));
  3. For hamburger/drawer button:

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);
    }
}

  • 有关ActionMenuItemView(工具栏上的按钮,包括溢出按钮):

  • For ActionMenuItemView (toolbar's buttons including overflow button):

    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);
            }
        });
    }
    

  • 有关溢出菜单中的项目文本:看看这个链接

    这篇关于如何动态地改变所有工具栏的图标颜色而不造型工具栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆