0

我有一個AppBarLayout內的v7工具欄,當我嘗試從漢堡圖標切換它時,它不會切換抽屜,除非我打開它,在此之後漢堡包正常工作。如果以前沒有打開抽屜,漢堡包不會切換

下面的代碼包含佈局內容和工具欄相關的代碼裏面的onCreate:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context=".MainActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="@color/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay"/> 

    </android.support.design.widget.AppBarLayout> 

    <include layout="@layout/content_main" /> 
toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar);  
drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
drawer.setDrawerListener(toggle); 
toggle.syncState(); 

還我試圖使它的工作由我自己添加NavigationOnClickListener,雖然它打印記錄它不打開抽屜(我有和沒有syncState()調用試了一下):

toolbar.setNavigationOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Log.i(TAG, "OPEN"); 
       toggle.syncState(); 
       drawer.openDrawer(GravityCompat.START); 
      } 
     }); 

回答

0

檢查這些代碼,如果你有沒有包括他們的是,試戴:

@Override 
protected void onPostCreate(Bundle savedInstanceState) { 
    super.onPostCreate(savedInstanceState); 
    toggle.syncState(); 
} 

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 
    toggle.onConfigurationChanged(newConfig); 
} 

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

除了onCreate,這些代碼也是每the official reference需要。

+0

你好哈塔,我已經有我的代碼,它不工作:( – Giovanny

相關問題