1

我剛剛在Android Studio中創建了一個模板項目,並未更改一行代碼(DrawerLayout模板項目)。即使在ActionBarDrawerToggle中設置了圖標,也無法更改DrawerLayout圖標

但是,我發現顯示導航片段的點擊圖標始終是左箭頭,即使在代碼中它將圖標設置爲另一個圖標。

screenshot

下面是構建ActionBarDrawerToggle代碼:

mDrawerToggle = new ActionBarDrawerToggle(
       getActivity(),     /* host Activity */ 
       mDrawerLayout,     /* DrawerLayout object */ 
       R.drawable.ic_drawer,    /* nav drawer image to replace 'Up' caret */ 
       R.string.navigation_drawer_open, /* "open drawer" description for accessibility */ 
       R.string.navigation_drawer_close /* "close drawer" description for accessibility */ 
     ) 

下面是ic_drawer

icon

PNG文件如何改變DrawerLayout圖標?

+0

你可以嘗試清理項目,然後運行? –

+0

對不起....它不工作.. – Qing

回答

2

我有同樣的問題,我總是看到左箭頭圖標。 我已經解決了這種方式:

我注意到,android.support.v4.app.ActionBarDrawerToggle類已棄用,但以下一些答案和developer.android.com,在我NavigationDrawerFragment類我已經使用

mDrawerToggle = new ActionBarDrawerToggle( getActivity(),     
              mDrawerLayout,      
              R.string.navigation_drawer_open, 
              R.string.navigation_drawer_close 
            ) { ... } 

現在沒問題,僅當導航抽屜出現時,圖標纔會轉換爲箭頭。

+0

好像你是從構造函數丟失的東西?你的工具欄在哪裏?所以你在工具欄中設置圖標還是什麼? – Qing

1

添加由Anchor給出的解決方案。我設法改變這種進口

import android.support.v4.app.ActionBarDrawerToggle; 

,使礦山工作

import android.support.v7.app.ActionBarDrawerToggle; 

否則會提示你在ActionBarDrawerToggle的構造函數的錯誤。刪除R.drawable.ic_drawer使之成爲:

mDrawerToggle = new ActionBarDrawerToggle(
      getActivity(),     /* host Activity */ 
      mDrawerLayout,     /* DrawerLayout object */ 
      R.string.navigation_drawer_open, /* "open drawer" description for accessibility */ 
      R.string.navigation_drawer_close /* "close drawer" description for accessibility */ 
    ) 

它應該工作現在

相關問題