【问题标题】:How to animate the hamburger icon to back arrow while using design support library?如何在使用设计支持库时将汉堡图标动画化为后退箭头?
【发布时间】:2015-07-06 21:17:32
【问题描述】:

您好,我正在使用来自 android 的最新支持库,称为设计支持库,并使用其中的 NavigationView 来展示抽屉。但问题是当我打开抽屉时,我的汉堡包图标不会旋转成后退箭头图标,它始终保持不变,但我记得当我使用没有支持库的抽屉布局时,它会自动旋转,这是我最后一次尝试我做了:

dl.setDrawerListener(new ActionBarDrawerToggle(this, dl, tb, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);

                supportInvalidateOptionsMenu();
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                supportInvalidateOptionsMenu();
            }

            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                super.onDrawerSlide(drawerView, slideOffset);

            }
        });

但没有发生任何事情,我在这里遗漏了什么小技巧或技巧吗?

【问题讨论】:

标签: android android-layout androiddesignsupport


【解决方案1】:

这里是绑定工具栏、抽屉布局以及如何同步它们的完整代码。

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.my_drawer_layout);
        Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);

        setSupportActionBar(toolbar);
        ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, drawer,
                toolbar, R.string.drawer_open, R.string.drawer_close);

        drawer.setDrawerListener(drawerToggle);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        drawerToggle.syncState();

【讨论】:

    【解决方案2】:

    您可以添加这些行。

    mDrawerLayout.post(new Runnable() {
            @Override
            public void run() {
               mActionBarDrawerToggle.syncState(); //Create a ActionBarDrawerToggle object instead of using a anonymous class in set drawerlistener
            }
        });
    

    希望对你有帮助。

    【讨论】:

      【解决方案3】:

      我已经通过这个解决了我的问题,实际上我正在使用一种方法来使用此代码将汉堡图标硬设置为操作栏图标:

      ab.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
      

      我刚刚注释掉了这行代码,然后就成功了。

      【讨论】:

        【解决方案4】:

        我制作了一个类,您可以使用它来制作汉堡包或菜单图标的动画。

        import android.app.Activity;
        import android.content.Context;
        import android.support.v7.graphics.drawable.DrawerArrowDrawable;
        
        /**
        * Created by ankush38u on 5/13/2016.
        */
        public class DrawerArrowAnimation {
        public static class DrawerArrowDrawableToggle extends DrawerArrowDrawable       implements DrawerToggle {
            private final Activity mActivity;
        
            public DrawerArrowDrawableToggle(Activity activity, Context themedContext) {
                super(themedContext);
                mActivity = activity;
            }
        
            public void setPosition(float position) {
                if (position == 1f) {
                    setVerticalMirror(true);
                } else if (position == 0f) {
                    setVerticalMirror(false);
                }
                setProgress(position);
            }
        
            public float getPosition() {
                return getProgress();
            }
        }
        
        /**
         * Interface for toggle drawables. Can be public in the future
         */
          public static interface DrawerToggle {
        
            public void setPosition(float position);
        
            public float getPosition();
        }
        
        }
        

        然后在activity或fragment中为这个动画drawable创建一个变量,并通过.setHomeAsUpIndicator(drawerDrawable)将drawable设置为home icon的drawable资源

        public static DrawerArrowAnimation.DrawerArrowDrawableToggle drawerDrawable;
        
        
        //this is if you are using fragments
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
        drawerDrawable = new DrawerArrowAnimation.DrawerArrowDrawableToggle(((AppCompatActivity) getActivity()), ((AppCompatActivity) getActivity()).getSupportActionBar().getThemedContext());
            ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            ((AppCompatActivity) getActivity()).getSupportActionBar().setHomeButtonEnabled(true);
            ((AppCompatActivity) getActivity()).getSupportActionBar().setHomeAsUpIndicator(drawerDrawable);
        

        现在你可以使用drawerDrawable.setPosition(float position); setPosition 从 0.0f 到 1.0f 为可绘制图标设置动画。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-03-02
          • 2015-01-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多