43

我正在使用新DrawerLayout的进行侧面导航。我正在使用这样的抽屉图标(“汉堡包”):

@Override
protected void onStart() {
    super.onStart();

    mDrawerLayout = (DrawerLayout) findViewById(R.id.activity_main_drawerlayout);
    mDrawerToggle = new ActionBarDrawerToggle(
            this, 
            mDrawerLayout, 
            R.drawable.ic_navigation_drawer, 
            R.string.app_name, 
            R.string.app_name);
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);
}

但是,当我将 a 添加Fragment到后台堆栈时,我想再次显示后退箭头,以便用户可以导航回“主页”,然后才打开应用程序抽屉。

如何将抽屉图标重置为后退图标?
我想要的箭头:

导航抽屉

4

2 回答 2

72

要禁用和隐藏 DrawerToggle “Hamburger”,只需调用

mDrawerToggle.setDrawerIndicatorEnabled(false);
于 2013-07-19T15:50:35.557 回答
2

我为托管活动创建了一个界面来更新汉堡菜单的视图状态。对于顶级片段,我将切换设置为true,对于要显示向上 < 箭头的片段,我将切换设置为false

public class SomeFragment extends Fragment {

    public interface OnFragmentInteractionListener {
        public void showDrawerToggle(boolean showDrawerToggle);
    }

    private OnFragmentInteractionListener mListener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            this.mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        mListener.showDrawerToggle(false);
    }
}

然后在我的活动中......

public class MainActivity extends Activity implements SomeFragment.OnFragmentInteractionListener {

    private ActionBarDrawerToggle mDrawerToggle;

    public void showDrawerToggle(boolean showDrawerIndicator) {
        mDrawerToggle.setDrawerIndicatorEnabled(showDrawerIndicator);
    }

}
于 2014-09-05T01:17:59.377 回答