2013-10-29 175 views
8

我使用的是搜索栏操作栏过了,我需要使用的动作条ICO像后退按钮:如何关闭导航抽屉以使用Back-home-icon按钮?

enter image description here

但我使用抽屉式导航栏太...哪有我解散/隐藏/禁用导航抽屉菜单以使用后退按钮?

我动作条代码:

@Override 
public boolean onCreateOptionsMenu(Menu menu){ 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.main, menu); 

    this.getSupportActionBar().setDisplayShowCustomEnabled(true); 
    this.getSupportActionBar().setDisplayShowTitleEnabled(false); 

    this.getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    this.getSupportActionBar().setHomeButtonEnabled(true); 

    LayoutInflater inflator = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View v; 

    if(!searchView){ 
     v = inflator.inflate(R.layout.action_textview, null); 
     ((TextView) v.findViewById(R.id.titleText)).setText(actionTitle); 
     menu.getItem(0).setVisible(true); 
     menu.getItem(2).setVisible(true); 
     mainMenu = menu; 

    }else{ 
     v = inflator.inflate(R.layout.action_searchview, null); 
     actionSearch = (EditText) v.findViewById(R.id.searchText); 
     actionSearch.setOnEditorActionListener(new OnEditorActionListener() { 
      @Override 
      public boolean onEditorAction(TextView view, int actionId, KeyEvent event) { 
       int result = actionId & EditorInfo.IME_MASK_ACTION; 
       switch(result) { 
       case EditorInfo.IME_ACTION_DONE: 
       case EditorInfo.IME_ACTION_NEXT: 
       case EditorInfo.IME_ACTION_GO: 
       case 0: 
        ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(actionSearch.getWindowToken(), 0); 
        String temp = actionSearch.getText().toString(); 
        searchFor(temp); 
        break; 
       } 
       return true; 
      } 
     }); 
     actionSearch.requestFocus(); 
     menu.getItem(0).setVisible(false); 
     menu.getItem(2).setVisible(false); 
    } 
    this.getSupportActionBar().setCustomView(v);   

    return super.onCreateOptionsMenu(menu); 
} 

代码帮助:

  1. 我的标题与只有一个TextView自定义视图,可自定义字体颜色和大小; (action_textview)
  2. 我的SearchBar使用一个自定义视图只有一个EditText; (action_searchview)

回答

11

ActionBarDrawerToggle您可以使用方法setDrawerIndicatorEnabled(Boolean enable)。或者,您可以设置ActionBar显示选项。特别是国旗DISPLAY_HOME_AS_UPLink to docs

然后像往常一样处理点击事件。

+0

太棒了!谢谢! :) –