如何从 Android appcompat v7 21 库中实现 DrawerArrowToggle [英] How to implement DrawerArrowToggle from Android appcompat v7 21 library

查看:24
本文介绍了如何从 Android appcompat v7 21 库中实现 DrawerArrowToggle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android 5.0 发布后,我想知道如何实现动画操作栏图标.

So now that Android 5.0 was released i was wondering how to implement the animated actionbar icons.

这个库 here 对我来说很好,但是因为 appcompat v7 库有它怎么能实施了吗?

This library here implements it fine for me but since the appcompat v7 library has it how can it be implemented?

库在 themes.xml 中引用它

The library references it in themes.xml

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

在这种风格下

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

更新

我使用 v7 DrawerToggle 实现了这一点.但是我不能设计它.请帮忙

I got this implemented using the v7 DrawerToggle. However I cannot style it. Please Help

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

I found the styling for it in the 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

I added this to my styles and did not work. Also added to my 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>

但是这样做时会崩溃并显示颜色类型错误.我错过了什么?

But crashes and says color type error when doing so. What am i missing?

推荐答案

首先,您应该知道现在 android.support.v4.app.ActionBarDrawerToggle 已被弃用.

First, you should know now the android.support.v4.app.ActionBarDrawerToggle is deprecated.

您必须将其替换为 android.support.v7.app.ActionBarDrawerToggle.

这是我的示例,我使用新的 Toolbar 替换 ActionBar.

Here is my example and I use the new Toolbar to replace the ActionBar.

MainActivity.java

public class MainActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);
    DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(
        this,  mDrawerLayout, mToolbar,
        R.string.navigation_drawer_open, R.string.navigation_drawer_close
    );
    mDrawerLayout.setDrawerListener(mDrawerToggle);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    mDrawerToggle.syncState();
}

styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>

<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
    <item name="spinBars">true</item>
    <item name="color">@android:color/white</item>
</style>

您可以阅读AndroidDocument#上的文档DrawerArrowToggle_spinBars

该属性是实现菜单到箭头动画的关键.

This attribute is the key to implement the menu-to-arrow animation.

public static int DrawerArrowToggle_spinBars

在过渡期间条形是否应旋转
必须是布尔值,真"或假".

public static int DrawerArrowToggle_spinBars

Whether bars should rotate or not during transition
Must be a boolean value, either "true" or "false".

所以,你设置了这个:true.

So, you set this: <item name="spinBars">true</item>.

然后就可以呈现动画了.

Then the animation can be presented.

希望能帮到你.

这篇关于如何从 Android appcompat v7 21 库中实现 DrawerArrowToggle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆