汉堡图标不显示在导航抽屉碎片中

vik*_*iki 7 android android-fragments navigation-drawer

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

public class NavigationDrawerFragment extends Fragment{
....
Run Code Online (Sandbox Code Playgroud)

调用syncState()activityCreated

@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();
}
Run Code Online (Sandbox Code Playgroud)

使用ic_drawerfor图标

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


    };
Run Code Online (Sandbox Code Playgroud)

无法弄清楚出了什么问题.请帮忙!

小智 13

我也有这个问题.对我来说,导入android.support.v7.app.ActionBarDrawerToggle而不是android.support.v4.app.ActionBarDrawerToggle修复它.您还必须R.drawable.ic_drawer在定义mDrawerToggle时删除参数:

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

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

  • @Suchith我遇到了保存问题,我覆盖了错误的公共void onPostCreate(Bundle savedInstanceState,PersistableBundle persistentState),它必须被保护void onPostCreate(Bundle savedInstanceState) (2认同)

agl*_*our 6

接受的答案是没有为我解决,我发现我对onPostCreate有错误的覆盖

@Override
public void onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
    super.onPostCreate(savedInstanceState, persistentState);
    mDrawerToggle.syncState();
}
Run Code Online (Sandbox Code Playgroud)

肯定是

@Override
public void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}
Run Code Online (Sandbox Code Playgroud)

  • 是的我也这样做了,但仍然没有工作 (2认同)