2014-12-21 203 views
0

我的問題很清楚,我想更改Up圖標當NavigationDrawer打開和關閉時。所以請告訴我如何在anction bar中更改upp圖標。我已經嘗試了很多,但我無法做到。 ii使用自定義佈局來設置標題。在此先感謝如何更改關閉或打開的圖標導航抽屜

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

     ActionBar actionBar = getSupportActionBar(); 
     actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
     actionBar.setCustomView(R.layout.center_action_bar_text); 
     actionBar.setTitle("dvds"); 
     View view = actionBar.getCustomView(); 
     TextView textView = (TextView) view.findViewById(R.id.title); 
     textView.setText("zfdgfdg"); 
     actionBar.setDisplayShowHomeEnabled(true); 
     actionBar.setDisplayHomeAsUpEnabled(true); 
     getActionBar().setHomeButtonEnabled(true); 


     --------------------------------- 
     drawerListView.setAdapter(drawerListAdapter); 
     DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 


     drawerToggle = new ActionBarDrawerToggle(HomeActivity.this, 
       drawerLayout, R.drawable.abc_ic_clear_search_api_holo_light, R.string.drawer_open, 
       R.string.drawer_close){ 

        @Override 
        public void onDrawerClosed(View drawerView) { 
         super.onDrawerClosed(drawerView); 
        } 

        @Override 
        public void onDrawerOpened(View drawerView) { 
         super.onDrawerOpened(drawerView); 
        } 
     }; 
     drawerLayout.setDrawerListener(drawerToggle); 
    } 
    @Override 
    protected void onPostCreate(Bundle savedInstanceState) { 
     super.onPostCreate(savedInstanceState); 
     drawerToggle.syncState(); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     if (drawerToggle.onOptionsItemSelected(item)) { 
      return true; 
      } 
     return super.onOptionsItemSelected(item); 
    } 

I want to use this image when drawer open I want to use this image when drawer closed

+0

你能否更具體?你想爲打開和關閉狀態設置哪些圖像?抽屜打開和關閉時是否還需要滑動動畫? –

回答

1

此方法添加到您的代碼:

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 
    drawerToggle.onConfigurationChanged(newConfig); 
} 
+0

完美答案。 – mjosh

+0

這隻會導致切換重新加載最初設置的圖像。我不認爲這是OP的要求。 –

1

我有另外一個答案

只需添加這個方法: -

@Override 
    public boolean onPrepareOptionsMenu(Menu menu) { 
     // if nav drawer is opened, hide the action items 
     boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList); 
     //boolean drawerOpen1 = mDrawerLayout.isDrawerOpen(this.slider); 
     //menu.findItem(R.id.sliding).setVisible(!drawerOpen); 
     if(drawerOpen) 
     { 
      menu.findItem(R.id.sliding).setIcon(android.R.drawable.ic_menu_close_clear_cancel); 
     } 
     else { 
      menu.findItem(R.id.sliding).setIcon(R.drawable.menuicon); 
     } 
     //menu.findItem(R.id.sliding).setIcon(R.drawable.ic_launcher); 
     //menu.findItem(R.id.action_settings).setVisible(!drawerOpen1); 
     return super.onPrepareOptionsMenu(menu); 
    } 

爲這個方法叫你需要使用此方法拖

invalidateOptionsMenu(); 
// calling onPrepareOptionsMenu() to hide action bar icons 

當菜單打開

public void onDrawerOpened(View drawerView) { 
     getActionBar().setTitle(mDrawerTitle); 
     //mDrawerLayout.openDrawer(Gravity.END); 
     // calling onPrepareOptionsMenu() to hide action bar icons 
     invalidateOptionsMenu(); 
    } 

菜單時接近

public void onDrawerClosed(View view) { 
       getActionBar().setTitle(mTitle); 
       //menu.findItem(R.id.sliding).; 
       //mDrawerLayout.closeDrawer(Gravity.END); 
       // calling onPrepareOptionsMenu() to show action bar icons 

       invalidateOptionsMenu(); 
      }