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

android-如何在不设置样式栏的情况下动态更改所有ToolBar图标的颜色,第1张

概述我一直在寻找一种方法来动态更改工具栏(如ActionBar)中所有元素的颜色.规格:>在styles.xml上使用parent=“Theme.AppCompat.Light.NoActionBar”>Appcompatv722>在我的AppCompatActivity中设置setSupportActionBar()>我从POST请求中获得了颜色(通常是#FF——格式)我已阅读

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

规格:

>在styles.xml上使用parent =“ theme.AppCompat.light.NoActionbar”
> Appcompat v7 22
>在我的AppCompatActivity中设置setSupportActionbar()
>我从POST请求中获得了颜色(通常是#FF ——格式)

我已阅读以下帖子:

> How do I change the color of the ActionBar hamburger icon?
> How to change color of hamburger icon in material design navigation drawer
> Can’t change navigation drawer icon color in android
> ActionBarDrawerToggle v7 arrow color
> Android Toolbar color change
> Android burger/arrow icon dynamic change color(这在某种程度上可行,但我不喜欢使用自己的无动画图片).
以及与此主题相关的其他链接…没有一个对我有用.

我现在正在做的是在工具栏(Get reference to drawer toggle in support actionbar)上搜索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);    }}

我使用以下工具更改背景和文本颜色:工具栏.setBackgroundcolor和工具栏.setTitleTextcolor.

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

MenuItem item2 = mMenu.findItem(R.ID.actionbar_group_moreoverflow);item2.getIcon().setcolorFilter(color, PorterDuff.Mode.SRC_Atop);

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

解决方法:

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

>对于背景颜色: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);    }}

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

>对于溢出菜单的项目文本:take a look at this link

总结

以上是内存溢出为你收集整理的android-如何在不设置样式栏的情况下动态更改所有ToolBar图标的颜色全部内容,希望文章能够帮你解决android-如何在不设置样式栏的情况下动态更改所有ToolBar图标的颜色所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1089914.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存