我在我的项目工具栏中使用Appcompat 7并使用导航切换。除了在每个活动或片段发生变化时动态更改 DrawerArrowToggle 图标的颜色的要求外,一切都有效。
我的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>
在上面的样式文件中,我使用 DrawerArrowToggle 颜色作为白色,但我的要求是在运行时更改为其他颜色。我没有发布任何代码,因为我完全陷入困境,而且根本无法找到符合我要求的单一代码。
答案 0 :(得分:3)
我不确定这是否可行,我没有测试过自己。
首先获取导航图标的view reference:
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;
}
获得视图参考后,将ColorFilter
应用于drawable,在这种情况下,ActionBarDrawerToggle图标:
View navigationIcon = getToolbarNavigationIcon(mToolbar);
Drawable navDrawable = navigationIcon.getDrawable();
if(navDrawable != null){
navDrawable.setColorFilter(newColor, PorterDuff.Mode.MULTIPLY);
}