【问题标题】:Hamburger Icon does not show in Navigation Drawer Fragment导航抽屉片段中不显示汉堡图标
【发布时间】:2015-07-17 20:25:01
【问题描述】:

我正在使用 Fragment 作为导航抽屉,因此无法使用 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()
        }


    };

不知道哪里出了问题。 请帮忙!

【问题讨论】:

    标签: android android-fragments navigation-drawer


    【解决方案1】:

    我也有这个问题。对我来说,导入 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 )

    如果这不起作用,请尝试this question 的一些答案。

    【讨论】:

    • 我正在使用 android.support.v7.app.ActionBarDrawerToggle 但没有运气任何想法?
    • @Suchith 我遇到了保存问题,我覆盖了错误的 public void onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState),它必须受到保护 void onPostCreate(Bundle savedInstanceState)
    • 这对我不起作用,尽管我的代码和逻辑与问题非常相似。但我有这条线的区别:mActionBarDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_item_close)
    【解决方案2】:

    接受的答案并没有为我解决,我发现我的 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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-01
      • 1970-01-01
      • 2016-02-02
      • 1970-01-01
      • 1970-01-01
      • 2022-08-15
      • 1970-01-01
      • 2015-12-24
      相关资源
      最近更新 更多