2013-10-29 30 views
8

Tôi đang sử dụng thanh tác vụ có thanh tìm kiếm quá, và tôi cần phải sử dụng các ico ActionBar giống như một nút Back:Làm cách nào để loại bỏ Ngăn Điều hướng để sử dụng nút Back-home-icon?

enter image description here

Nhưng tôi đang sử dụng ngăn điều hướng quá ... Làm thế nào có thể Tôi loại bỏ/ẩn/vô hiệu hóa menu Ngăn Điều hướng để sử dụng nút quay lại?

đang ActionBar của tôi:

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

Mã giúp đỡ:

  1. Tiêu đề của tôi một cái nhìn tùy chỉnh của nó chỉ với một TextView, để tùy chỉnh màu chữ và kích thước; (action_textview)
  2. Thanh tìm kiếm của tôi sử dụng chế độ xem tùy chỉnh chỉ với EditText; (action_searchview)

Trả lời

11

Từ ActionBarDrawerToggle bạn có thể sử dụng phương pháp setDrawerIndicatorEnabled(Boolean enable). Ngoài ra, bạn có thể đặt các tùy chọn Hiển thị ActionBar. Cụ thể là cờ DISPLAY_HOME_AS_UP. Link to docs

Sau đó, xử lý sự kiện nhấp như bình thường.

+0

Hoạt động tuyệt vời! Cảm ơn! :) –

Các vấn đề liên quan