如何为 Android appcompat v7 21 库中的 DrawerArrowToggle 添加样式

33

现在Android 5.0已经发布,我想知道如何为动画操作栏图标设置样式。

这个对于我来说实现和样式都很好,但既然appcompat v7库也有它,那么该如何设置样式呢?

我使用了v7 DrawerToggle进行了实现,但是我无法为其设置样式。请帮忙。

我在v7 styles_base.xml中找到了它的样式。

    <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>

我将这个添加到我的样式表中,但没有起作用。还将它添加到了我的attr.xml文件中。

我加入这个到我的样式但没效果。也加到我的attr.xml里了。

<declare-styleable name="DrawerArrowToggle">
    <!-- The drawing color for the bars -->
    <attr name="color" format="color"/>
    <!-- Whether bars should rotate or not during transition -->
    <attr name="spinBars" format="boolean"/>
    <!-- The total size of the drawable -->
    <attr name="drawableSize" format="dimension"/>
    <!-- The max gap between the bars when they are parallel to each other -->
    <attr name="gapBetweenBars" format="dimension"/>
    <!-- The size of the top and bottom bars when they merge to the middle bar to form an arrow -->
    <attr name="topBottomBarArrowSize" format="dimension"/>
    <!-- The size of the middle bar when top and bottom bars merge into middle bar to form an arrow -->
    <attr name="middleBarArrowSize" format="dimension"/>
    <!-- The size of the bars when they are parallel to each other -->
    <attr name="barSize" format="dimension"/>
    <!-- The thickness (stroke size) for the bar paint -->
    <attr name="thickness" format="dimension"/>
</declare-styleable>
但在执行此操作时,会崩溃并出现颜色类型错误。我缺少什么?
7个回答

101

以下方法适用于我:

<style name="MyTheme" parent="Theme.AppCompat">
   <item name="drawerArrowStyle">@style/MyDrawerArrowToggle</item>
</style>

<style name="MyDrawerArrowToggle" parent="Widget.AppCompat.DrawerArrowToggle">
  <item name="color">@color/your_color</item>
</style>

1
我的基础主题是 holo 而不是 appcompat。我不支持 API 低于 15 的设备。如果将 appcompat 主题作为我的基础主题,那么所有的东西都会改变。有没有什么办法可以解决这个问题? - BigDX
1
好的,我已经将所有内容转换为appcompat,将我的FragmentActivity更改为ActionBarActivity,将getActionBar更改为getSupportActionBar并创建了一个工具栏。然后我使用了你上面提供的代码,但仍然无法正常工作。甚至不会改变颜色或动画。我可能做错了什么? - BigDX
我也看了你的工作(非常棒)在这里 https://chris.banes.me/2014/10/17/appcompat-v21/ 但是还是没有运气。 - BigDX
由于某些原因,这只改变了汉堡图标的颜色,但没有改变抽屉的颜色。我需要设置colorControlNormal属性来同时更改两者。请参见我的答案https://dev59.com/8F8d5IYBdhLWcg3wxkpq#28450500 - marwinXXII
这段程序可以在5.1.1 - API 22上工作,但无法在4.2.2 - API 17上工作。尽管我已经在values-v14中添加了以上解决方案。有人知道我该怎么做才能让它在API 14-API 22上运行吗? - Shirish Herwade
@ShirishHerwade 在你定义工具栏的 XML 代码中,是否写着 android:theme="@style/Toolbar_Theme"?如果是的话,请尝试将其更改为 app:theme="@style/Toolbar_Theme" - Paul M.

25

在我的情况下,我想要改变抽屉箭头和汉堡包图标的颜色。 设置抽屉箭头样式只改变了汉堡包图标的颜色。

因此,我打开了appcompat-v7中的values.xml文件中的Widget.AppCompat.DrawerArrowToggle样式。

<style name="Widget.AppCompat.DrawerArrowToggle" parent="Base.Widget.AppCompat.DrawerArrowToggle">
    <item name="color">?attr/colorControlNormal</item>
</style>

所以我创建了特别的主题:

<style name="Toolbar_Theme">
    <item name="colorControlNormal">@android:color/black</item>
</style>

我按照以下步骤将它应用到我的工具栏:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    app:theme="@style/Toolbar_Theme" />

请注意,我正在使用 theme 属性,而不是在我的应用程序主题中定义 controlColorNormal。这样,颜色仅适用于工具栏项,如果我在应用程序主题中设置它,那么它还会影响滚动条的颜色等。

设置colorControlNormal 属性将同时改变汉堡包和抽屉箭头的颜色。


这正是我也想做的事情,但出于某种原因,它对我没有任何改变。 - Barodapride

3

如果有人(就像我一样)想要使用v7 ActionBarDrawerToggle将抽屉指示器图标替换为自己的可绘制图形(非动画),可以按照以下步骤操作:

//After instantiating your ActionBarDrawerToggle
mDrawerToggle.setDrawerIndicatorEnabled(false);
Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.your_custom_icon, getActivity().getTheme());
mDrawerToggle.setHomeAsUpIndicator(drawable);
mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (mDrawerLayout.isDrawerVisible(GravityCompat.START)) {
            mDrawerLayout.closeDrawer(GravityCompat.START);
        } else {
            mDrawerLayout.openDrawer(GravityCompat.START);
        }
    }
});

0

对于我的情况,操作栏的主题与切换白色和暗色汉堡图标相关:

<item name="actionBarWidgetTheme">@style/Theme.AppCompat</item>

对比。

<item name="actionBarWidgetTheme">@style/Theme.AppCompat.Light</item>

0

我想把汉堡/箭头变成白色,但是所有的“样式”都不起作用。 我把所有的东西都删掉了,只加了这一行,就解决了问题,将其作为“android.support.v7.widget.Toolbar”元素在布局中的属性:

android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar


-1

您的样式名称没有父级。 这是在使用Android 5和Material主题时的操作,无需用appCompat替换actionbar。 这是我的样式,我只需要将颜色更改为白色,因为我有一个彩色的栏。

     <style name="myTheme.DrawerArrowToggle" parent="Base.Widget.AppCompat.DrawerArrowToggle">
    <item name="color">@color/white</item>
</style>

这是我的主题

<style name="Theme_AppTheme.Light" parent="@android:style/Theme.Material.Light">

     <!-- Drawer\arrow in white -->
     <item name="drawerArrowStyle">@style/myTheme.DrawerArrowToggle</item>

附言:存在一个问题,在Android 4.4上,普通ActionBar无法正常呈现。不幸的是,来自v7的新drawerToggle没有继承v4。


-1

不确定具体的错误是什么,如果你收到了“color already defined”的定义,那么请前往attrs.xml搜索attr name="color"并将其重命名或注释掉以进行测试。你可能需要将自定义属性更改为其他名称,我不确定这是否是支持库的问题,但这对我有效。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接