2015-02-24 66 views
8

我試圖在我的應用中實現android.support.v4.app.ActionBarDrawerToggle;因爲此類已棄用已過時的ActionBarDrawerToggle

此課程已棄用。請在 support-v7-appcompat中使用ActionBarDrawerToggle。

我已經切換到android.support.v7.app.ActionBarDrawerToggle。

之前,我可以打電話給這樣的構造:

mDrawerToggle = new ActionBarDrawerToggle(
      this,     /* host Activity */ 
      mDrawerLayout,   /* DrawerLayout object */ 
      R.drawable.ic_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() 
     } 
    }; 

,但我已經切換到新的V7支持庫後,我收到錯誤

"ActionBarDrawerToggle() in ActionBarDrawerToggle cannot be applied to: 

toolbar: android.support.v7.widget.Toolbar 
Actual arguments: R.drawable.ic_drawer (int)" 

顯然我不是介紹一個合適的工具欄到構造函數中,但我不確定要理解兩個衝突參數之間的區別。我如何獲得所需的工具欄?

+1

我第二個這個問題。我發現這是一個部分解決方案:getActionBar()。setDisplayShowHomeEnabled(true); getActionBar()。setIcon(R.drawable.ic_drawer); 問題是圖標有填充 - 我該如何刪除它?另外 - 我在哪裏可以找到一個很好的ic_drawer.png? – mikeesouth 2015-03-11 22:54:27

+1

同時,這個問題在http://stackoverflow.com/questions/26439619/how-to-replace-deprecated-android-support-v4-app-actionbardrawertoggle回答 – k3b 2015-04-14 19:12:20

回答

17

我通過導入新android.support.v7.app.ActionBarDrawerToggle,通過使用RecyclerView而不是ListView控件的解決我的問題,如本例所示:How to make Material Design Navigation Drawer With Header View

private ActionBarDrawerToggle mDrawerToggle; 
//... ... 
mDrawerToggle = new ActionBarDrawerToggle(
      this, 
      mDrawerLayout, 
      toolbar, 
      R.string.drawer_open, R.string.drawer_close){ 
        @Override 
        public void onDrawerOpened(View drawerView) { 
         super.onDrawerOpened(drawerView); 
         // code here will execute once the drawer is opened 
         getSupportActionBar().setTitle(mTitle); 
         invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 
        } 
        @Override 
        public void onDrawerClosed(View drawerView) { 
         super.onDrawerClosed(drawerView); 
         // Code here will execute once drawer is closed 
         getSupportActionBar().setTitle(mDrawerTitle); 
         invalidateOptionsMenu(); 
    }; 

如果您仍然有問題點擊這裏: How to replace deprecated android.support.v4.app.ActionBarDrawerToggle