汉堡图标不会在导航抽屉片段展示会在、抽屉、图标、片段

2023-09-05 10:41:51 作者:想暧昧却动了心

我使用的是Fragement导航抽屉,因此不能使用onPostCreated调用syncState

I am using a Fragement for navigation drawer and hence cant use onPostCreated to call syncState

public class NavigationDrawerFragment extends Fragment{
....

调用syncState()上avctivityCreated

Calling syncState() on avctivityCreated

@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的图标

Using the ic_drawer for icon

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


    };

无法弄清楚什么是错的。 请帮忙

Can't figure out what is wrong. Please help

推荐答案

我有这个问题为好。对我来说,进口 android.support.v7.app.ActionBarDrawerToggle 而不是 android.support.v4.app.ActionBarDrawerToggle 固定它。您还可以在定义mDrawerToggle删除 R.drawable.ic_drawer 参数:

I had this problem as well. For me, importing android.support.v7.app.ActionBarDrawerToggle instead of android.support.v4.app.ActionBarDrawerToggle fixed it. You also have to remove the R.drawable.ic_drawer argument when you define mDrawerToggle:

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

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

如果还是不行,请尝试一些答案为this问题。

If that doesn't work, try some of the answers to this question.