1

您好,我正在使用来自 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);

            }
        });

但什么也没发生,我在这里有什么小技巧或技巧吗?

4

4 回答 4

4

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

        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();
于 2015-07-06T21:47:32.040 回答
1

您可以添加这些行。

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

希望能帮助到你。

于 2015-07-06T21:44:55.807 回答
0

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

ab.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);

我刚刚注释掉了这行代码,然后它就起作用了。

于 2015-07-06T21:41:19.000 回答
0

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

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();
}

}

然后为此动画drawable在activity或fragment中创建一个变量,并通过 .setHomeAsUpIndicator(drawerDrawable) 将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 为可绘制图标设置动画。

于 2016-05-13T11:24:38.270 回答