2016-04-09 8 views
-2

Androidのアクションバーの上ボタンアイコンを変更してナビゲーションドロワーメニューアイコンを表示したいとします。私はこれを試して、それは動作するようだが、それを行う正しい方法ですか?Androidでアクションバーの上ボタンアイコンを変更するにはどうすればよいですか?

private void configureToolbar() { 

    if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) { 
     mToolbar = (Toolbar) findViewById(R.id.drawer_toolbar); 
     mToolbar.setTitle(getResources().getString(R.string.navigation_drawer_title)); 
     setSupportActionBar(mToolbar); 

     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setHomeButtonEnabled(true); 
    } else { 
     mToolbar = (Toolbar) findViewById(R.id.toolbar); 
     mToolbar.setTitle(getResources().getString(R.string.officer_activity_title)); 
     setSupportActionBar(mToolbar); 

     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setHomeButtonEnabled(true); 
     getSupportActionBar().setHomeAsUpIndicator(R.drawable.menu); 
    } 

    setDrawerToggle(); 

} 

これはandroid.support.v4.app.ActionBarDrawerToggleは廃止され、新しいsupport.v7 classあなたは、そのコンストラクタにアイコン描画可能に合格することはできませんことを検討しています。

答えて

0

迫元android.support.v7.app.ActionBarDrawerToggleと使用ツールバー

toolbar = (Toolbar) findViewById(R.id.toolbar); 

     if (toolbar != null) { 
      setSupportActionBar(toolbar); 
     } 

    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(); 
     }; 

はい、私はそれを行うツールバー

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:local="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:minHeight="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" 
    local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
    /> 
+0

ためのXMLのコードの下に追加します。ツールバーのホームアイコンを矢印アイコンからメニューアイコンに変更したいだけです。 –

関連する問題