0

在寫這個線程之前,我嘗試實現了我在stackoverflow中找到的不同解決方案,但沒有任何工作正常。如何更改Android中的漢堡包圖標(NavigationDrawer)

我正在開發一個使用自定義導航抽屜的Android應用程序,我必須更改操作欄的標準圖標(現在是工具欄,對吧?)和設置圖標。

這是我的代碼:

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

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     toolbar.setTitleTextColor(Color.parseColor("#009754")); 
     setSupportActionBar(toolbar); 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 
    } 

而這正是我試圖實現:

此解決方案不起作用:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
toolbar.setTitleTextColor(Color.parseColor("#009754")); 
toolbar.setNavigationIcon(R.drawable.ic_draw); 
setSupportActionBar(toolbar); 

此解決方案不起作用:

getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(false); 
toggle.setDrawerIndicatorEnabled(false); 
toggle.setHomeAsUpIndicator(R.drawable.ic_custom_drawer_icon); 

我不在乎t和爲什麼我不能更改圖標,我不知道是什麼問題...

ActionBarDrawerToggle

回答

2

簡單而優雅解決方案

廣場

getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeAsUpIndicator(R.drawable.dashboardicon); //你的圖標這裏

後在導航活動

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      this, drawer,toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
    drawer.addDrawerListener(toggle); 
    toggle.syncState(); 

    getSupportActionBar().setHomeButtonEnabled(true); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    getSupportActionBar().setHomeAsUpIndicator(R.drawable.dashboardicon); 

    navigationView= (NavigationView) findViewById(R.id.nav_view); 
    navigationView.setNavigationItemSelectedListener(this); 

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      this, drawer,toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
    drawer.addDrawerListener(toggle); 
    toggle.syncState(); 

整體解決方案:它沒有需要任何setNavigationOnClickListener()

1

禁用抽屜指標:

toggle.setDrawerIndicatorEnabled(false); 

然後:

toolbar.setNavigationIcon(R.drawable. ic_custom_drawer_icon); 
+1

然後通過調用'setNavigationOnClickListener(添加自定義的監聽器)'上工具欄如[這裏]所述(https://stackoverflow.com/a/32695966/876267)。 – Mehmed

相關問題