【问题标题】:Open navigation drawer by clicking the app icon通过单击应用程序图标打开导航抽屉
【发布时间】:2013-11-22 06:08:51
【问题描述】:

我想让我的用户通过单击应用程序图标来打开导航抽屉。这是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add);
    // Show the Up button in the action bar.

    DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.left_drawer);
    ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(
            this,                  /* host Activity */
            mDrawerLayout, /* DrawerLayout object */
           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 */
            ) {

        /** 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);
        }
    };


    mDrawerLayout.setDrawerListener(mDrawerToggle);
    getActionBar().setDisplayHomeAsUpEnabled(true); // Pressing the app icon in the action bar will navigate to the parent activity.
    getActionBar().setHomeButtonEnabled(true);

}

但是当我点击图标时,什么也没有发生。问题出在哪里。

【问题讨论】:

    标签: android navigation-drawer


    【解决方案1】:

    您应该覆盖活动的onOptionsItemSelected 并使用它:

    @Override
        public boolean onOptionsItemSelected(MenuItem item) {
    
            if (mDrawerToggle.onOptionsItemSelected(item)) {
                return true;
            }
            return super.onOptionsItemSelected(item);
    }
    

    【讨论】:

      【解决方案2】:

      在这里查看example of the docs。您需要在

      中添加其他代码
      • onPostCreate() 同步你的抽屉状态
      • onOptionsItemSelected()处理App图标的触摸事件
      • onConfigurationChanged() 为抽屉提供新配置

        public class YourActivity extends Activity {
        
        public ActionBarDrawerToggle mDrawerToggle;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            ...
            mDrawerToggle = new ActionBarDrawerToggle();
            ...
        }
        
        @Override
        protected void onPostCreate(Bundle savedInstanceState) {
             super.onPostCreate(savedInstanceState);
             // Sync the toggle state after onRestoreInstanceState has occurred.
             mDrawerToggle.syncState();
        }
        
        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            mDrawerToggle.onConfigurationChanged(newConfig);
        }
        
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
             // Pass the event to ActionBarDrawerToggle, if it returns
             // true, then it has handled the app icon touch event
             if (mDrawerToggle.onOptionsItemSelected(item)) {
                 return true;
             }
             // Handle your other action bar items...
        
             return super.onOptionsItemSelected(item);
        }
        
        }
        

      【讨论】:

      • 谢谢。当我添加此代码时,它说 mDrawerToogle 无法解析。我试着这样写:“public ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle”但没有帮助。我对java不是很有经验。
      • @user2971688 将 ActionBarDrawerToggle 作为 Activity 中的一个字段,并相应地更改 onCreate 中的代码部分。
      • 我添加了您的 onPostCreate()、onConfigurationChanged 和您的 onOptionsItemSelected,但它显示“无法解析 mDrawerToggle”?
      • @user2971688 仔细查看 onCreate 和上面的行。
      • ActionBarDrawerToogle 没有上述构造函数正确的构造函数是:mDrawerToggle = new ActionBarDrawerToggle(activity,drawerLayout,drawerImageRes,openDrawerContentDescRes,closeDrawerContentDescRes)
      猜你喜欢
      • 2013-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多