gpt4 book ai didi

android - 更改 Android 工具栏 DrawerArrowToggle 颜色

转载 作者:行者123 更新时间:2023-11-29 17:39:15 25 4
gpt4 key购买 nike

我在我的项目中将 Appcompat 7 用于带有导航切换的工具栏。除了要求在每个 Activity 或 fragment 发生变化时动态更改 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 颜色作为 White,但我的要求是在运行时更改为其他颜色。我没有发布任何代码,因为我完全被卡住了,甚至找不到满足我要求的一段代码。

最佳答案

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

首先获取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;
}

获得 View 引用后,将 ColorFilter 应用于可绘制对象,在本例中为 ActionBarDrawerToggle 图标:

View navigationIcon = getToolbarNavigationIcon(mToolbar);
Drawable navDrawable = navigationIcon.getDrawable();
if(navDrawable != null){
navDrawable.setColorFilter(newColor, PorterDuff.Mode.MULTIPLY);
}

关于android - 更改 Android 工具栏 DrawerArrowToggle 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29346672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com