如何从Android appcompat v7 21库实现DrawerArrowToggle

如何从Android appcompat v7 21库实现DrawerArrowToggle

所以现在Android 5.0发布了,我想知道如何实现动画操作栏图标。

这个库在这里实现它很好但是因为appcompat v7库有它如何实现它?

该库在themes.xml中引用它

 <item name="drawerArrowStyle">@style/Widget.AppCompat.DrawerArrowToggle</item>

在这种风格下

 <style name="Base.V7.Theme.AppCompat" parent="Platform.AppCompat">

UPDATE

我使用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

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

但是崩溃并且在这样做时说颜色类型错误。我错过了什么?


杨魅力
浏览 712回答 3
3回答

FFIVE

要回答问题的更新部分:要设置抽屉图标/箭头的样式,您有两种选择:设计箭头本身为此,请drawerArrowStyle在主题中覆盖,如下所示:<style&nbsp;name="AppBaseTheme"&nbsp;parent="Theme.AppCompat.Light"> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="drawerArrowStyle">@style/MyTheme.DrawerArrowToggle</item></style><style&nbsp;name="MyTheme.DrawerArrowToggle"&nbsp;parent="Widget.AppCompat.DrawerArrowToggle"> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="color">@android:color/holo_purple</item> &nbsp;&nbsp;&nbsp;&nbsp;<!--&nbsp;^&nbsp;this&nbsp;will&nbsp;make&nbsp;the&nbsp;icon&nbsp;purple&nbsp;--></style>这可能不是你想要的,因为ActionBar本身应该与箭头具有一致的样式,所以,最有可能的是,你想要选项二:主题ActionBar /工具栏使用您自己的主题(您可能应该从中派生出来)覆盖全局应用程序主题的android:actionBarTheme(actionBarThemefor appcompat)属性,ThemeOverlay.Material.ActionBar/ThemeOverlay.AppCompat.ActionBar如下所示:<style&nbsp;name="AppBaseTheme"&nbsp;parent="Theme.AppCompat.Light"> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="actionBarTheme">@style/MyTheme.ActionBar</item></style><style&nbsp;name="MyTheme.ActionBar"&nbsp;parent="ThemeOverlay.AppCompat.ActionBar"> &nbsp;&nbsp;&nbsp;&nbsp;<item&nbsp;name="android:textColorPrimary">@android:color/white</item> &nbsp;&nbsp;&nbsp;&nbsp;<!--&nbsp;^&nbsp;this&nbsp;will&nbsp;make&nbsp;text&nbsp;and&nbsp;arrow&nbsp;white&nbsp;--> &nbsp;&nbsp;&nbsp;&nbsp;<!--&nbsp;you&nbsp;can&nbsp;also&nbsp;override&nbsp;drawerArrowStyle&nbsp;here&nbsp;--></style>这里一个重要的注意事项是,当使用自定义布局Toolbar而不是库存ActionBar实现时(例如,如果您使用DrawerLayout-&nbsp;NavigationView-&nbsp;Toolbar组合来实现在半透明状态栏下可见的材质样式抽屉效果),该actionBarTheme属性显然不是自动拾取(因为它意味着由AppCompatActivity默认值处理ActionBar),因此对于您的自定义,Toolbar请不要忘记手动应用您的主题:<!--inside&nbsp;your&nbsp;custom&nbsp;layout&nbsp;with&nbsp;DrawerLayout and&nbsp;NavigationView&nbsp;or&nbsp;whatever&nbsp;--><android.support.v7.widget.Toolbar &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;app:theme="?actionBarTheme">-&nbsp;ThemeOverlay.AppCompat.ActionBar如果在派生主题中设置属性,这将解析为AppCompat的默认值或覆盖。PS关于drawerArrowStyle覆盖和spinBars属性的一点评论- 许多消息来源建议应该设置true为获取抽屉/箭头动画。spinBars事实上true&nbsp;,默认情况下,它在AppCompat中(检查Base.Widget.AppCompat.DrawerArrowToggle.Common样式),您根本不必重写actionBarTheme以使动画正常工作。即使您覆盖它并将属性设置为动态,也可以获得动画false,它只是一个不同的,不那么圆润的动画。这里重要的是使用ActionBarDrawerToggle,它是花哨的动画drawable的东西。
打开App,查看更多内容
随时随地看视频慕课网APP