汉堡图标在导航抽屉片段中未显示

8

我正在使用一个片段来创建导航抽屉,因此无法使用onPostCreated调用syncState。

public class NavigationDrawerFragment extends Fragment{
....

activityCreated 中调用 syncState()

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    // Indicate that this fragment would like to influence the set of actions in the action bar.
    setHasOptionsMenu(true);
    mDrawerToggle.syncState();
}

使用ic_drawer来代替图标。
public void setUp(int fragmentId, DrawerLayout drawerLayout) {
    mFragmentContainerView = getActivity().findViewById(fragmentId);
    mDrawerLayout = drawerLayout;

    // set a custom shadow that overlays the main content when the drawer opens
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
    // set up the drawer's list view with items and click listener

    ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setHomeButtonEnabled(true);

    // ActionBarDrawerToggle ties together the the proper interactions
    // between the navigation drawer and the action bar app icon.
    mDrawerToggle = new ActionBarDrawerToggle(
            getActivity(),                    /* host Activity */
            mDrawerLayout,                    /* DrawerLayout object */
            R.drawable.ic_drawer,             /* nav drawer image to replace 'Up' caret */
            R.string.navigation_drawer_open,  /* "open drawer" description for accessibility */
            R.string.navigation_drawer_close  /* "close drawer" description for accessibility */
    ) {
        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            if (!isAdded()) {
                return;
            }

            getActivity().supportInvalidateOptionsMenu(); // calls onPrepareOptionsMenu()
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            if (!isAdded()) {
                return;
            }

            if (!mUserLearnedDrawer) {
                // The user manually opened the drawer; store this flag to prevent auto-showing
                // the navigation drawer automatically in the future.
                mUserLearnedDrawer = true;
                SharedPreferences sp = PreferenceManager
                        .getDefaultSharedPreferences(getActivity());
                sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true).apply();
            }

            getActivity().supportInvalidateOptionsMenu(); // calls onPrepareOptionsMenu()
        }


    };

无法确定问题出在哪里。 请帮忙!

2个回答

15

我也遇到过这个问题。对于我来说,使用 android.support.v7.app.ActionBarDrawerToggle 替代 android.support.v4.app.ActionBarDrawerToggle 可以解决此问题。您还需要在定义 mDrawerToggle 时删除 R.drawable.ic_drawer 参数:

mDrawerToggle = new ActionBarDrawerToggle( getActivity(), mDrawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close )

如果这不起作用,请尝试一些回答此问题的答案:这个问题


我正在使用android.support.v7.app.ActionBarDrawerToggle,但是没有运气,有什么想法吗? - Suchith
2
@Suchith,我曾遇到相同的问题,是因为我覆盖了错误的public void onPostCreate(Bundle savedInstanceState,PersistableBundle persistentState)方法,它应该是protected void onPostCreate(Bundle savedInstanceState)。 - aglour
尽管我的代码和逻辑与问题非常相似,但这对我不起作用。但是我有这行代码的区别: mActionBarDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_item_close) - blueware

10

对我来说,被接受的答案没有解决我的问题,后来我发现我的 onPostCreate 覆盖方法是错误的。

@Override
public void onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
    super.onPostCreate(savedInstanceState, persistentState);
    mDrawerToggle.syncState();
}

必须是这样

@Override
public void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接