如何更改导航抽屉DrawerArrowToggle的图标?

时间:2015-03-30 12:58:42

标签: android navigation-drawer android-appcompat

你好我正在使用AppCompat v7导航抽屉。我需要更改没有动画的汉堡菜单图标。

enter image description here

我经历了DrawerArrowToggle的风格并找到了这个

<style name="Base.Widget.AppCompat.DrawerArrowToggle" parent="">
        <item name="color">?android:attr/textColorSecondary</item>
        <item name="thickness">2dp</item>
        <item name="barSize">18dp</item>
        <item name="gapBetweenBars">3dp</item>
        <item name="topBottomBarArrowSize">11.31dp</item>
        <item name="middleBarArrowSize">16dp</item>
        <item name="drawableSize">24dp</item>
        <item name="spinBars">true</item>
    </style>

并且没有可绘制属性来设置自定义图标。有没有办法改变图标?

我知道我们可以禁用drawerToggle并在工具栏中设置自定义图片并保持打开和关闭状态。

1 个答案:

答案 0 :(得分:1)

我认为如果您使用 v7 ActionBarDrawerToggle,目前无法做到这一点。这个类只有一个包本地构造函数,允许你更改汉堡菜单/滑块。

来源:https://android.googlesource.com/platform/frameworks/support/+/master/v7/appcompat/src/android/support/v7/app/ActionBarDrawerToggle.java

/**
 * In the future, we can make this constructor public if we want to let developers customize the animation.
 */
<T extends Drawable & DrawerToggle> ActionBarDrawerToggle(Activity activity, Toolbar toolbar, DrawerLayout drawerLayout, T slider, @StringRes int openDrawerContentDescRes, @StringRes int closeDrawerContentDescRes) {
    // some code
    if (slider == null) {
        mSlider = new DrawerArrowDrawableToggle(activity,
        mActivityImpl.getActionBarThemedContext());
    } else {
       mSlider = slider;
   }

}

作为(坏)解决方法,我建议您继续使用旧的 v4 ActionBarDrawerToggle,它允许自定义汉堡图标。

请注意,您可以按照此SO注释中的说明自定义默认动画滑块:https://stackoverflow.com/a/26447144/2960788

此外,您可以通过设置

来设置滑块的颜色
<item name="colorControlNormal">#ccffffff</item>

在工具栏的主题中。