19

我正在尝试将新的 DrawerLayout 用于列表。问题是虽然我设置了抽屉监听器,但操作栏上的指示器仍然是箭头图标,而不是我打算绘制的 3 行图标。以下是 OnCreate 函数:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_front_page);

    // Swiping Pager set up
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    // Sliding Drawer set up
    mHabitContract = new HabitsContract(this);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.front_page_layout);
    mDrawerList = (ListView) findViewById(R.id.habit_list);
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
    mDrawerList.setAdapter(new HabitAdapter(mHabitContract.GetHabitItems(), this));

    // Fixme: Indicator image doesn't show up
    mDrawerToggle = new ActionBarDrawerToggle(
            this,                  /* host Activity */
            mDrawerLayout,         /* DrawerLayout object */
            R.drawable.ic_navigation_drawer,  /* nav drawer image to replace 'Up' caret */
            R.string.drawer_open,  /* "open drawer" description for accessibility */
            R.string.drawer_close  /* "close drawer" description for accessibility */
    ) {
        public void onDrawerClosed(View view) {
            //getActionBar().setTitle(mTitle);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        public void onDrawerOpened(View drawerView) {
            //getActionBar().setTitle(mDrawerTitle);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    // Action Bar set up
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);
    getActionBar().setDisplayShowHomeEnabled(true);
}

任何人都可以帮忙吗?

更新:我发现了问题。我按如下方式添加了 onPostCreate 函数,现在它可以工作了。

protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}
4

4 回答 4

35

你需要syncState()调用你的ActionBarDrawerToggle对象onPostCreate()

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}
于 2013-08-22T12:19:28.730 回答
3

对于那些正在搜索或来这里寻找答案的人:您必须使用 DrawerToggle support.v7。按照我的使用方式:

drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_opened, R.string.drawer_closed){
        public void onDrawerClosed(View view){
            //actionBar.setTitle(mTitle);
            invalidateOptionsMenu(); // crea la llamada para onPrepareOptionsMenu
        }
        public void onDrawerOpened(View view){
            //actionBar.setTitle(mDrawerTitle);
            invalidateOptionsMenu(); // crea la llamda para onPrepareOptionsMenu
        }
    };

这很好用,并且有一个摇滚的动画!

于 2015-08-11T15:18:51.217 回答
0

您使用的图标称为“ic_navigation_drawer”。您可以检查它是否正确,因为 Google 在官方指南 ( http://developer.android.com/training/implementing-navigation/nav-drawer.html ) 中提供的图标称为“ic_drawer”。

于 2013-07-24T05:38:36.427 回答
0

还要仔细检查自定义主题是否已在带有条件的文件ActionBar中定义到您的项目中。像这样:style.xmlandroid:displayOptions=showTitle

 <style name="OrangeBar" parent="@android:style/Widget.Holo.ActionBar">
    <item name="android:displayOptions">showTitle</item>
    ...
 </style>

它会破坏抽屉指示器。为了避免这种情况showHome,请添加android:displayOptions

    <item name="android:displayOptions">showHome|showTitle</item>
于 2013-12-09T15:58:27.323 回答