更改Android工具栏抽屉箭头切换颜色

更改Android工具栏抽屉箭头切换颜色,android,menuitem,material-design,android-toolbar,drawertoggle,Android,Menuitem,Material Design,Android Toolbar,Drawertoggle,我在我的项目中使用Appcompat 7作为带有导航切换的工具栏。除了要求在每个活动或片段更改时动态更改抽屉RowToggle图标的颜色外,所有功能都正常工作 我的styles.xml文件代码如下: <style name="NavigationTheme" parent="Theme.AppCompat.Light"> <item name="colorPrimary">#FFFFF</item> <item name=

我在我的项目中使用Appcompat 7作为带有导航切换的工具栏。除了要求在每个活动或片段更改时动态更改抽屉RowToggle图标的颜色外,所有功能都正常工作

我的
styles.xml
文件代码如下:

<style name="NavigationTheme" parent="Theme.AppCompat.Light">
        <item name="colorPrimary">#FFFFF</item>
        <item name="colorPrimaryDark">#F2F2F2</item>
        <item name="android:windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>


<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
        <item name="spinBars">false</item>
        <item name="color">#FFFFFF</item>
    </style>

#FFFFF
#F2F2F2
真的
假的
@样式/抽屉箭头样式
假的
#FFFFFF


在上面的样式文件中,我使用了drawerRowToggle颜色作为白色,但我的要求是在运行时更改为其他颜色。我没有发布任何代码,因为我完全被卡住了,没有地方可以找到满足我需求的一段代码。

我不确定这是否可行,我还没有测试自己

首先获取导航图标的名称:

 public static View getToolbarNavigationIcon(Toolbar toolbar){
        //check if contentDescription previously was set
        boolean hadContentDescription = TextUtils.isEmpty(toolbar.getNavigationContentDescription());
        String contentDescription = !hadContentDescription ? toolbar.getNavigationContentDescription() : "navigationIcon";
        toolbar.setNavigationContentDescription(contentDescription);
        ArrayList<View> potentialViews = new ArrayList<View>();
        //find the view based on it's content description, set programatically or with android:contentDescription
        toolbar.findViewsWithText(potentialViews,contentDescription, View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
        //Nav icon is always instantiated at this point because calling setNavigationContentDescription ensures its existence 
        View navIcon = null;
        if(potentialViews.size() > 0){
            navIcon = potentialViews.get(0); //navigation icon is ImageButton
        }
         //Clear content description if not previously present
        if(hadContentDescription)
            toolbar.setNavigationContentDescription(null);
        return navIcon;
     }

从可访问性的角度来看,清除内容描述是一个坏主意。也许您可以使用视图的标记来存储一些东西,以表明它已经被更改了?事实上。如果以前未设置此代码段中的contentDescription,则会清除该代码段中的contentDescription。只需使用(在kotlin中)`toolbar.navigationIcon?.apply{if(这是DrawerArrowDrawable)color=/*yourColor*/}`我知道这是一个迟来的答案,但以防将来有人需要它。
View navigationIcon = getToolbarNavigationIcon(mToolbar);
Drawable navDrawable = navigationIcon.getDrawable();
if(navDrawable != null){
   navDrawable.setColorFilter(newColor, PorterDuff.Mode.MULTIPLY);
}