1

我在 oncreate 函数中使用此代码通过按应用程序图标打开导航抽屉。

ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                (DrawerLayout) findViewById(R.id.left_drawer), /* DrawerLayout object */
                getResources().getDrawable(R.drawable.ic_drawer),  /* nav drawer icon to replace 'Up' caret */
                getString(R.string.drawer_open),  /* "open drawer" description */
                getString(R.string.drawer_close)  /* "close drawer" description */
                ) {

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(R.string.title_activity_add);
            }

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(R.string.drawer_title);
            }
        };

现在它说“构造函数 ActionBarDrawerToggle(AddActivity, DrawerLayout, Drawable, String, String) 未定义”。我已经导入了 android.support.v4.app.ActionBarDrawerToggle。问题出在哪里?

4

2 回答 2

4

构造函数不是ActionBarDrawerToggle(AddActivity, DrawerLayout, Drawable, String, String)。它是ActionBarDrawerToggle(Activity, DrawerLayout, int, int, int)。将最后三个参数更改为资源 ID,而不是引用资源 ID 的结果。

于 2013-11-09T18:43:42.317 回答
0

只是为了回应@CommonsWare 的回答。而不是findViewById(R.id.left_drawer)仅仅做R.id.left_drawer

所以最终的结果应该是这样的:

ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description */
                R.string.drawer_close  /* "close drawer" description */
                )
于 2020-03-22T20:50:39.423 回答