2015-06-13 177 views
26

我有一個簡單的android.support.v7.widget.Toolbar,我試圖做的是按左上角的「漢堡包」圖標打開一個NavigationDrawer。 「漢堡包」按鈕是可見的,當我從左邊開始拉動時,我看到按鈕上的動畫,但按下按鈕並不像我期望的那樣打開/關閉NavigationDrawer。我已經關注了[Google文檔] [1],但仍然無法弄清楚。對不起,任何混亂,下面是簡化代碼我目前正在嘗試使用:單擊工具欄上的漢堡包圖標無法打開導航抽屜

public class MainActivity extends AppCompatActivity implements 
    View.OnClickListener, 
    GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener { 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 


    setContentView(R.layout.activity_main); 


    Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar); 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 



    toolbar.setNavigationOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Log.d("NICK", "button button button.................."); 
     } 
    }); 

     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer); 
     NavigationView n = (NavigationView) findViewById(R.id.nav); 
     mDrawerLayout.setOnClickListener(new View.OnClickListener(){ 
      @Override 
      public void onClick(View v) { 
       Log.d("NICK", "button button button.................."); 
      } 
     }); 

     //mDrawerLayout.setDrawerListener(mDrawerToggle); 
     n.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 
      @Override 
      public boolean onNavigationItemSelected(MenuItem menuItem) { 
       switch (menuItem.getItemId()) { 
        ////....... 

       } 
       mDrawerLayout.closeDrawers(); // CLOSE DRAWER 
       return true; 
      } 
     }); 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    Log.d("NICK","CWECNEWKVNERIPNVIEWNFVIPEWNVIPEWNVPIEWNVPIEWNVPIEWNVPIRWNVPRWVPO"); 
    switch (item.getItemId()) { 
     case android.R.id.home: 
      mDrawerLayout.openDrawer(GravityCompat.START); // OPEN DRAWER 
      Log.d("NICK","CWECNEWKVNERIPNVIEWNFVIPEWNVIPEWNVPIEWNVPIEWNVPIEWNVPIRWNVPRWVPO"); 
      return true; 

    } 
      return super.onOptionsItemSelected(item); 


} 

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



@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.drawer, menu); 
    return true; 
} 


} 

}

而且因爲它是運行時,我沒有得到任何日誌調試語句。

這實質上是我擁有的問題:https://stackoverflow.com/a/26636045/1489990。我遵循這一點,它只是不起作用。

我的理解是,當按下漢堡包圖標時,會調用setNavigationOnClickListener,這是我關注的焦點,我的努力是正確處理事件,因爲當我按下按鈕時,我沒有得到我的日誌語句。讓我知道這個想法是不正確的。 https://developer.android.com/reference/android/widget/Toolbar.html#setNavigationOnClickListener(android.view.View.OnClickListener)

我的佈局:

ActivityMain.xml

<RelativeLayout 
xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/d" 
android:background="@drawable/home_wall"> 




<android.support.v7.widget.Toolbar 
    android:id="@+id/my_toolbar" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:minHeight="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" 
    android:layout_marginBottom="10dp" 
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
    android:layout_marginTop="25dp" 

    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" /> 



<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:id="@+id/drawer" 

    android:layout_height="match_parent" 
    android:fitsSystemWindows="true"> 



    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="200dp" 
     android:id="@+id/imageView" 
     android:src="@drawable/trans2" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="10dp" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:paddingBottom="300dp" /> 

    <RelativeLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/d8" 
     android:layout_alignParentTop="true" 
     android:layout_alignLeft="@+id/imageView" 
     android:layout_alignStart="@+id/imageView" 
     android:paddingTop="0dp"> 

     <Button 
      android:layout_width="75dp" 
      android:layout_height="50dp" 
      android:text="Gallery" 
      android:id="@+id/save_button" 
      android:background="#dd2c00" android:textColor="#fff" 
      android:layout_below="@+id/Purchases" 
      android:layout_toRightOf="@+id/start_button" 
      android:layout_toEndOf="@+id/start_button" /> 
     <Button 
      android:layout_width="125dp" 
      android:layout_height="50dp" 
      android:text="Store" 
      android:id="@+id/Purchases" 
      android:background="#ff6e40" android:textColor="#fff" 
      android:layout_above="@+id/instructions_button6" 
      android:layout_toLeftOf="@+id/start_button" 
      android:layout_toStartOf="@+id/start_button" 
      android:layout_marginBottom="98dp" /> 
     <Button 
      android:layout_width="75dp" 
      android:layout_height="50dp" 
      android:text="Help" 
      android:id="@+id/instructions_button6" 
      android:background="#dd2c00" android:textColor="#fff" 
      android:layout_alignParentBottom="true" 
      android:layout_toLeftOf="@+id/start_button" 
      android:layout_toStartOf="@+id/start_button" 
      android:layout_marginLeft="5dp" 
      android:layout_marginBottom="10dp" /> 
     <Button 
      android:layout_width="75dp" 
      android:layout_height="300dp" 
      android:text="Start" 
      android:id="@+id/start_button" 
      android:background="#ff3d00" 
      android:textColor="#fff" 
      android:layout_alignParentBottom="true" 
      android:layout_centerHorizontal="true" 
      android:layout_marginBottom="10dp" /> 
     <Button 
      android:layout_width="125dp" 
      android:layout_height="50dp" 
      android:text="Achievements" 
      android:id="@+id/Scores" 
      android:background="#ff6e40" android:textColor="#fff" 
      android:layout_alignTop="@+id/Purchases" 
      android:layout_toRightOf="@+id/start_button" 
      android:layout_toEndOf="@+id/start_button" /> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Welcome to the quiz!" 
      android:id="@+id/textView" 
      android:textColor="#fff" 
      android:textSize="20dp" 
      android:layout_alignParentTop="true" 
      android:layout_centerHorizontal="true" 
      android:layout_marginTop="70dp" /> 


     <!-- sign-in button --> 
     <com.google.android.gms.common.SignInButton 
      android:id="@+id/sign_in_button" 
      android:layout_width="110dp" 
      android:layout_height="50dp" 
      android:layout_above="@+id/start_button" 
      android:layout_centerHorizontal="true" 
      android:visibility="visible" /> 

     <!-- sign-out button --> 
     <Button 
      android:id="@+id/sign_out_button" 
      android:layout_width="125dp" 
      android:layout_height="wrap_content" 
      android:text="Sign Out" 
      android:visibility="invisible" 
      android:background="#dd4b39" 
      android:textColor="#fff" 
      android:layout_alignTop="@+id/sign_in_button" 
      android:layout_centerHorizontal="true" 
      android:layout_marginTop="160dp" /> 

    </RelativeLayout> 

    <android.support.design.widget.NavigationView 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:background="#fff" 
     android:id="@+id/nav" 

     app:headerLayout="@layout/drawer_header" 
     app:menu="@menu/drawer"/> 
</android.support.v4.widget.DrawerLayout> 

Drawer.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/drawer_menu" 
xmlns:app="http://schemas.android.com/apk/res-auto"> 

<item android:title="Google Play Games" 

    android:icon="@drawable/ic_local_airport_white_48dp"> 
    <menu> 
     <item 
      android:id="@+id/Sign_in_drawer" 

      android:icon="@drawable/games_controller_grey" 
      android:title="Sign in" /> 
     <item 
      android:id="@+id/ach" 
      android:icon="@drawable/games_achievements" 
      android:title="Achievements" /> 
    </menu> 
</item> 

<item android:title="Start a Quiz" 

    android:icon="@drawable/ic_local_airport_white_48dp"> 
    <menu> 
     <item 
      android:id="@+id/quizStart25" 

      android:icon="@drawable/ic_local_airport_white_48dp" 
      android:title="25 Questions" /> 
     <item 
      android:id="@+id/quizStart10" 
      android:icon="@drawable/ic_local_airport_white_48dp" 
      android:title="10 Questions" /> 
    </menu> 
</item> 

<group 
    android:checkableBehavior="single"> 
    <item 
     android:id="@+id/gallery" 

     android:icon="@drawable/ic_photo_library_white_48dp" 
     android:title="Gallery" /> 
    <item 
     android:id="@+id/stats" 
     android:icon="@drawable/ic_toc_white_48dp" 
     android:title="Statistics" /> 
    <item 
     android:id="@+id/store" 
     android:icon="@drawable/ic_shop_white_48dp" 
     android:title="Store" /> 
    <item 
     android:id="@+id/settings" 
     android:icon="@drawable/ic_settings_white_48dp" 
     android:title="Settings" /> 
    <item 
     android:id="@+id/about" 
     android:icon="@drawable/ic_info_white_48dp" 
     android:title="About" /> 

</group> 

<item android:title="Support"> 
    <menu> 
     <item 
      android:id="@+id/help_drawer" 
      android:icon="@drawable/ic_help_white_48dp" 
      android:title="Help" /> 
     <item 
      android:id="@+id/report" 
      android:icon="@drawable/ic_report_problem_white_48dp" 
      android:title="Contact Developer" /> 
     <item 
      android:id="@+id/GPlusCommunity" 
      android:icon="@drawable/btn_g_white_normal" 
      android:title="Google+ Community" /> 

    </menu> 
</item> 

+0

問題是你如何創建你的佈局。工具欄位於DrawerLayout的外部,因此您的工具欄正在丟失上下文。看到我更新的答案。 – hungryghost

回答

78

見我answer到類似的職位。

基本上,只要跳過ActionBarDrawerToggle如果你不需要它的其他事情(如回調)。您可以刪除所有的代碼,只是實現你的開/關抽屜的行爲,就像這樣:

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case android.R.id.home: 
      mDrawerLayout.openDrawer(GravityCompat.START); // OPEN DRAWER 
      return true; 
     .... 
    } 
    return super.onOptionsItemSelected(item); 
} 

上面的代碼段的工作你是否正在使用NavigationView。如果您使用的是您的NavigationView點擊收聽導航視圖,:

navigationView.setNavigationItemSelectedListener(
    new NavigationView.OnNavigationItemSelectedListener() { 
     @Override 
     public boolean onNavigationItemSelected(MenuItem menuItem) { 
      // Handle menu item clicks here. 
      drawerLayout.closeDrawers(); // CLOSE DRAWER 
      return true; 
     } 
    }); 

更新:

此外,請確保您的活動正在擴大AppCompatActivity(而不是ActionBarActivity)。有關更多信息,請參閱my answer here

UPDATE2:

我調試您的修改後的代碼。嘗試這個。它現在應該工作。閱讀我的內聯的意見,看看有什麼我更改原因:

import android.os.Bundle; 
import android.support.design.widget.NavigationView; 
import android.support.v4.view.GravityCompat; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 

public class MainActivity extends AppCompatActivity { 
//  View.OnClickListener, 
//  GoogleApiClient.ConnectionCallbacks, 
//  GoogleApiClient.OnConnectionFailedListener { 


    DrawerLayout mDrawerLayout; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar); 
     setSupportActionBar(toolbar); 
     // You were missing this setHomeAsUpIndicator 
     getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

     // I removed your toolbar and drawer click listeners. 
     // They're not needed to open drawer. 
     // The drawer opens from onOptionsItemSelected(). 

     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer); 
     NavigationView n = (NavigationView) findViewById(R.id.nav); 
     n.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 
      @Override 
      public boolean onNavigationItemSelected(MenuItem menuItem) { 
       switch (menuItem.getItemId()) { 
        ////....... 

       } 
       mDrawerLayout.closeDrawers(); // CLOSE DRAWER 
       return true; 
      } 
     }); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     Log.d("NICK", "CWECNEWKVNERIPNVIEWNFVIPEWNVIPEWNVPIEWNVPIEWNVPIEWNVPIRWNVPRWVPO"); 
     switch (item.getItemId()) { 
      // THIS IS YOUR DRAWER/HAMBURGER BUTTON 
      case android.R.id.home: 
       mDrawerLayout.openDrawer(GravityCompat.START); // OPEN DRAWER 
       Log.d("NICK", "CWECNEWKVNERIPNVIEWNFVIPEWNVIPEWNVPIEWNVPIEWNVPIEWNVPIRWNVPRWVPO"); 
       return true; 

     } 
     return super.onOptionsItemSelected(item); 
    } 
} 

// I also removed your onConfigurationChanged(). 
// It's not needed since you're no longer using ActionBarDrawerToggle. 

UPDATE3:這個問題的佈局

看着你ActivityMain.xml的是,我看到您的工具欄是你DrawerLayout之外。你不能那樣做。 工具欄需要成爲DrawerLayout的子項,以便工具欄找到合適的上下文。

您需要使DrawerLayout成爲您活動的根源。這裏的documentation,具體報價爲:

要添加抽屜式導航,帶有 DrawerLayout對象作爲佈局的根視圖聲明你的用戶界面。在 DrawerLayout中,添加一個視圖,其中包含 屏幕(抽屜被隱藏時的主要佈局)和包含抽屜導航內容的另一個 的主要內容。希望

<android.support.v4.widget.DrawerLayout 
    android:id="@+id/drawer" 
    ... > 

    <RelativeLayout 
     android:id="@+id/d" 
     ...> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/my_toolbar" 
      ... /> 

     <!-- Your other content goes here --> 

    </RelativeLayout> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav" 
     ... /> 

</android.support.v4.widget.DrawerLayout> 

,這會對您的問題護理:

所以基本上,改變你的ActivityMain.xml到這個樣子。

+0

我已經嘗試過這一點,我甚至在'onOptionsItemSelected'中插入了日誌語句,它們從不顯示,表明這種方法永遠不會到達。因爲現在我在我的'onCreate'方法之外有'onOptionsItemSelected',如果這很重要的話。仍然沒有找到解決方案 – ez4nick

+0

@ ez4nick,看到我更新的答案。你有沒有機會擴展ActionBarActivity? – hungryghost

+1

我正在擴展'AppCompatActivity' – ez4nick

8

你需要同步的抽屜切換:

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

編輯:該代碼爲我工作(從您的文章複製)

public class TempActivity extends AppCompatActivity { 
    private ActionBarDrawerToggle mDrawerToggle; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.temp); 
     setupDrawer(); 
    } 
    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     mDrawerToggle.onConfigurationChanged(newConfig); 
    } 
    @Override 
    public boolean onOptionsItemSelected(final MenuItem item) { 
     if (mDrawerToggle.onOptionsItemSelected(item)) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
    @Override 
    protected void onPostCreate(Bundle savedInstanceState) { 
     super.onPostCreate(savedInstanceState); 
     mDrawerToggle.syncState(); 
    } 
    private void setupDrawer() { 
     Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setHomeButtonEnabled(true); 
     DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.my_drawer_layout); 
     mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,toolbar,R.string.drawer_open, R.string.drawer_close) { 
      public void onDrawerClosed(View view) { 
       super.onDrawerClosed(view); 
       invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 
      } 
      public void onDrawerOpened(View drawerView) { 
       super.onDrawerOpened(drawerView); 
       invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 
      } 
     }; 
     mDrawerLayout.setDrawerListener(mDrawerToggle); 
    } 
} 

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/my_drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 
     <android.support.v7.widget.Toolbar 
      android:id="@+id/my_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 
     <FrameLayout 
      android:id="@+id/content_frame" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 
    </LinearLayout> 
    <RelativeLayout 
     android:id="@+id/left_drawer" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:choiceMode="singleChoice" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="0dp" 
     android:background="#111"> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#FFF" 
      android:text="DRAMER MENU" /> 
    </RelativeLayout> 
</android.support.v4.widget.DrawerLayout> 

但如果您使用的是新NavigationView,那麼你不」 t需要撥動等Here is a good example如何使用它。

+0

我正在使用新的'NavigationView',我試着按照您提供的鏈接(請參閱更新2),並且永遠不會收到確認按鈕被按下。我假設這是NavigationDrawer實際打開/關閉的地方 – ez4nick

+0

然後不需要設置「ActionBarDrawerToggle」。 – mbmc

1

對於抽屜撥動你需要設置「顯示家庭高達」爲false: getSupportActionBar().setDisplayHomeAsUpEnabled(false);

+2

試過這個,它並沒有解決我的問題。和以前一樣的問題。 – ez4nick

1

因爲我沒有具體回答你的問題,我想提出一個完全不同的方法:

在我的項目中使用即時通訊由Mike朋茲開發的Material Drawer。 它看起來非常好,很容易實現,並沒有太多你必須擔心。

所以,如果你找不到解決你的問題,你可以試試看。

1

覆蓋onOptionsItemSelected方法和使用下面

if(item.getItemId() == android.R.id.home){ // use android.R.id 
    mDrawerLayout.openDrawer(Gravity.LEFT); 
} 
1

使用ActionBarDrawerToggle和實施public boolean onOptionsItemSelected(MenuItem item)

工具欄不必是DrawerLayout內,它不太可能是這個問題的原因。 toggle.onOptionsItemSelected(item)查找所選項目的ID,如果它是android.R.id.home,則抽屜的可見性會切換。因此,工具欄或任何創建Home MenuItem的View都可以放置在佈局的任何位置。

中的活動:

ActionBarDrawerToggle toggle; 

private void setupDrawerToggleInActionBar() { 
    // assuming a Toolbar has been initialized in your onCreate 
    this.setSupportActionBar(toolbar); 

    // setup the action bar properties that give us a hamburger menu 
    ActionBar actionBar = this.getSupportActionBar(); 
    if(actionBar != null) { 
     actionBar.setDisplayHomeAsUpEnabled(true); 
     actionBar.setHomeButtonEnabled(true); 
    } 

    // the toggle allows for the simplest of open/close handling 
    toggle = new ActionBarDrawerToggle(this, 
             drawerLayout, 
             R.string.navigation_drawer_open, 
             R.string.navigation_drawer_close); 
    // drawerListener must be set before syncState is called 
    drawerLayout.setDrawerListener(toggle); 

    toggle.setDrawerIndicatorEnabled(true); 
    toggle.syncState(); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    // This is required to make the drawer toggle work 
    if(toggle.onOptionsItemSelected(item)) { 
     return true; 
    } 

    /* 
    * if you have other menu items in your activity/toolbar 
    * handle them here and return true 
    */ 

    return super.onOptionsItemSelected(item); 
} 
1

簡單的通話功能onOptionsItemSelected ActionBarDrawerToggle類(菜單項菜單項)上的活動選項菜單管理像下面的功能。

mDrawerToggle= new ActionBarDrawerToggle(activity, drawerLayout, R.string.nav_drawer_accessbility_drawer_open, 
      R.string.nav_drawer_accessbility_drawer_close); 


@Override 
public boolean onOptionsItemSelected(final android.view.MenuItem item) { 
    navigation().getOptionsMenuInflater(this).closeSearchView(); 
    // The action bar home/up action should open or close the drawer. 
    // ActionBarDrawerToggle will take care of this behavior. 
    mDrawerToggle.onOptionsItemSelected(item); 

    return super.onOptionsItemSelected(item); 
} 

OR

覆蓋onOptionsItemSelected活動像下面

public boolean onOptionsItemSelected(MenuItem item) { 
    if (item != null && item.getItemId() == android.R.id.home) { 
     toggle(); 

    } 
    return super.onOptionsItemSelected(item); 
} 

private void toggle() { 
    if (mDrawerLayout.isDrawerVisible(GravityCompat.START)) { 
     mDrawerLayout.closeDrawer(GravityCompat.START); 
    } else { 
     mDrawerLayout.openDrawer(GravityCompat.START); 
    } 
} 
0

雖然接受的答案在這裏工作好了但是,正如他們所說的 「預防勝於治療」 ..添加mDrawerToggle.syncState( );在onPostCreate和onConfigurationChanged()wroks作爲answerd通過@mbmc上述好得多:

@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); 
} 
0

如果幫助別人,對我來說這事,因爲調用setSupportActionBar(toolbar)兩次的愚蠢錯誤的一樣。只需在onCreate方法中調用一次,就再也不用。我的錯誤是後來用另一種方法調用它。

相關問題