Android 如何更改所有工具栏';s图标在没有样式工具栏的情况下动态着色

Android 如何更改所有工具栏';s图标在没有样式工具栏的情况下动态着色,android,android-appcompat,android-toolbar,android-imagebutton,Android,Android Appcompat,Android Toolbar,Android Imagebutton,我一直在寻找一种方法来改变工具栏中所有元素的颜色,就像动态操作栏一样 规格: 在styles.xml上使用parent=“Theme.AppCompat.Light.NoActionBar” Appcompat v7 22 在myAppCompatActivity中设置setSupportActionBar() 我从一个帖子请求中得到了颜色(通常是FF格式) 我读过以下帖子: (这张图片在某种程度上起了作用,但我不喜欢使用没有动画的自己的图片)。 以及其他与此主题相关的链接。。。他

我一直在寻找一种方法来改变工具栏中所有元素的颜色,就像动态操作栏一样

规格:

  • 在styles.xml上使用
    parent=“Theme.AppCompat.Light.NoActionBar”
  • Appcompat v7 22
  • 在my
    AppCompatActivity中设置
    setSupportActionBar()
  • 我从一个帖子请求中得到了颜色(通常是FF格式)
我读过以下帖子:

  • (这张图片在某种程度上起了作用,但我不喜欢使用没有动画的自己的图片)。
    以及其他与此主题相关的链接。。。他们都没有为我工作。

  • 我现在正在做的是搜索工具栏上的ImageButton(),并将
    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);
        }
    }
    


    问题:是否有更好的方法(动态更改工具栏元素的颜色)?

    要获取所有工具栏视图,请遍历所有子视图并分别为其着色。它的循环代码如下所示:

    public static void colorizeToolbar(Toolbar toolbarView, int toolbarIconsColor, Activity activity) {
        final PorterDuffColorFilter colorFilter
                = new PorterDuffColorFilter(toolbarIconsColor, PorterDuff.Mode.MULTIPLY);
    
        for(int i = 0; i < toolbarView.getChildCount(); i++) {
            final View v = toolbarView.getChildAt(i);
    
            //Step 1 : Changing the color of back button (or open drawer button).
            if(v instanceof ImageButton) {
                //Action Bar back button
                ((ImageButton)v).getDrawable().setColorFilter(colorFilter);
            }
    
            if(v instanceof ActionMenuView) {
                for(int j = 0; j < ((ActionMenuView)v).getChildCount(); j++) {
    
                    //Step 2: Changing the color of any ActionMenuViews - icons that
                    //are not back button, nor text, nor overflow menu icon.
                    final View innerView = ((ActionMenuView)v).getChildAt(j);
    
                    if(innerView instanceof ActionMenuItemView) {
                        int drawablesCount = ((ActionMenuItemView)innerView).getCompoundDrawables().length;
                        for(int k = 0; k < drawablesCount; k++) {
                            if(((ActionMenuItemView)innerView).getCompoundDrawables()[k] != null) {
                                final int finalK = k;
    
                                //Important to set the color filter in seperate thread, 
                                //by adding it to the message queue
                                //Won't work otherwise.
                                innerView.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        ((ActionMenuItemView) innerView).getCompoundDrawables()[finalK].setColorFilter(colorFilter);
                                    }
                                });
                            }
                        }
                    }
                }
            }
    
            //Step 3: Changing the color of title and subtitle.
            toolbarView.setTitleTextColor(toolbarIconsColor);
            toolbarView.setSubtitleTextColor(toolbarIconsColor);
    
            //Step 4: Changing the color of the Overflow Menu icon.
            setOverflowButtonColor(activity, colorFilter);
        }
    }
    

    看看这个链接,它可能会帮助你

    我在这里也面临同样的问题。我对工具栏元素所做的操作:

  • 背景色:
    toolbar.setBackgroundColor(color.parseColor(“#xxxxxxxx”)
  • 对于文本颜色:
    toolbar.setTitleTextColor(color.parseColor(“#xxxxxxxx”)
  • 汉堡/抽屉按钮:

    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);
        }
    }
    
    int color=color.parseColor(#xxxxxxxx”);
    最终PorterDuffColorFilter colorFilter=新PorterDuffColorFilter(颜色,PorterDuff.Mode.SRC_);
    对于(int i=0;i
  • 对于ActionMenuItemView(包括溢出按钮在内的工具栏按钮):

    private void colorizeToolBarItem(AppCompatActivity活动,最终PorterDuffColorFilter colorFilter,最终字符串描述){
    最终视图组decorView=(视图组)activity.getWindow().getDecorView();
    final ViewTreeObserver ViewTreeObserver=decorView.getViewTreeObserver();
    viewTreeObserver.addOnGlobalLayoutListener(新viewTreeObserver.OnGlobalLayoutListener(){
    @凌驾
    公共图书馆{
    最终ArrayList OutView=新ArrayList();
    decorView.FindView WithText(输出视图、说明、,
    查看。查找带有内容描述的视图);
    if(outview.isEmpty())
    返回;
    ActionMenuItemView溢出=(ActionMenuItemView)outViews.get(0);
    溢出。getCompoundDrawables()[0]。setColorFilter(colorFilter);
    移除LobalLayoutListener(decorView,本);
    }
    });
    }
    
  • 对于溢出菜单的项目文本:


  • 我很想知道答案,但是你能给我解释一下这对我回答这个问题有什么帮助吗?(动态更改它)。不知道我是否不够清楚,或者你的答案中有什么我看不到的地方,我的意思是,我说的是动态的,没有风格。很抱歉,我已经删除了所有不需要的代码,检查答案中的链接它可能会帮到你!当然,我会添加必要的部分。使用它时,我遇到了一些关于PorterDuff.Mode的问题,我想我找到了一个轻松的方法来设置overflowbutton,让我完成我的代码,我会完成你的答案并将其标记为正确(如果一切正常)。处理完代码后,我给你一份简历:我改变了SRC_的PorterDebuff.Mode.MULTIPLY。我必须更改这一行
    ((ImageButton)v).getDrawable().setColorFilter(colorFilter)用于
    ((图像按钮)v)。设置颜色过滤器(颜色过滤器)因为第一个不工作。第2步和第4步不起作用,我在看我能做些什么……根据@Shishram的回答,我在做类似于你的事情。但仍然缺少如何为溢出菜单的背景和图标着色。
    
    private static void setOverflowButtonColor(final Activity activity, final PorterDuffColorFilter colorFilter) {
        final String overflowDescription = activity.getString(R.string.abc_action_menu_overflow_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<View>();
                decorView.findViewsWithText(outViews, overflowDescription,
                        View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
                if (outViews.isEmpty()) {
                    return;
                }
                TintImageView overflow=(TintImageView) outViews.get(0);
                overflow.setColorFilter(colorFilter);
                removeOnGlobalLayoutListener(decorView,this);
            }
        });
    }
    
    mToolbarView.setBackgroundColor(color);
    ToolbarColorizeHelper.colorizeToolbar(mToolbarView, mToolbarIconsColor, getActivity());
    
    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);
        }
    }
    
    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);
            }
        });
    }