您好我正在使用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);
}
});
但没有任何事情发生,我在这里有什么简短的技巧或提示吗?
答案 0 :(得分: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();
答案 1 :(得分:1)
您可以添加这些行。
mDrawerLayout.post(new Runnable() {
@Override
public void run() {
mActionBarDrawerToggle.syncState(); //Create a ActionBarDrawerToggle object instead of using a anonymous class in set drawerlistener
}
});
希望它有所帮助。
答案 2 :(得分:0)
我已经解决了我的问题,实际上我正在使用一种方法使用此代码将汉堡包图标硬设置为操作栏图标:
composer.json
我刚刚注释掉了这行代码,然后就运行了。
答案 3 :(得分: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创建活动或片段中的变量,并将drawable设置为主页图标的可绘制资源.setHomeAsUpIndicator(drawerDrawable)
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(浮动位置); setPosition从0.0f到1.0f为可绘制图标设置动画。