2014-11-13 41 views
21

Tôi đang cố thêm chế độ xem tùy chỉnh vào thanh công cụ mới (Lollipop). Nhưng bằng cách nào đó khung nhìn được thêm vào bên dưới thanh công cụ. Nó đã làm việc tốt khi tôi sử dụng actionBar.setCustomView nhưng bây giờ sau khi di chuyển đến thanh công cụ, nó không hoạt động. Dưới đây là mã. Những thay đổi nên được thực hiện?Thêm chế độ xem tùy chỉnh vào thanh công cụ

Fragment:

toolbar = (Toolbar) getView().findViewById(R.id.toolbar); 
    ((ActionBarActivity) getActivity()).setSupportActionBar(toolbar); 

    toolbar.setTitle(getString(R.string.app)); 



    ActionBar actionBar = ((ActionBarActivity) getActivity()) 
      .getSupportActionBar(); 

    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 

    LayoutInflater inflater = (LayoutInflater) getActivity() 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    // inflate the view 
    final View view = inflater.inflate(R.layout.actionbar_search, null); 
    final ImageView searchIcon = (ImageView) view 
      .findViewById(R.id.search_icon); 
    final ClearableAutoCompleteTextView searchBox = (ClearableAutoCompleteTextView) view 
      .findViewById(R.id.search_box); 

    // start with the text view hidden in the action bar 
    searchBox.setVisibility(View.INVISIBLE); 
    searchIcon.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      toggleSearch(false, view); 
     } 
    }); 

    searchBox.setOnClearListener(new OnClearListener() { 

     @Override 
     public void onClear() { 
      toggleSearch(true, view); 
     } 
    }); 

    searchBox.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 


     } 

    }); 

toolbar.addView(view);    
// actionBar.setCustomView(view); // This worked previously 
//((ActionBarActivity)getActivity()).getSupportActionBar().setCustomView(view); //doesnt work with toolbar 

Trả lời

40

Với thanh công cụ tôi đã quản lý để đạt được điều đó như thế này:

setSupportActionBar(toolbar); 
    View logo = getLayoutInflater().inflate(R.layout.view_logo, null); 
    toolbar.addView(logo); 

Hoặc bạn cũng có thể chỉ cần thêm cái nhìn của bạn vào thanh công cụ xml, vì nó chỉ là một ViewGroup . Bằng cách này, bạn sẽ có bản xem trước trong trình chỉnh sửa bố cục. Không yêu cầu mã java.

+6

Tôi đã thử sử dụng appcompat 'Toolbar.addView()' nhưng không hoạt động. – patrickjason91

+1

Nó không hoạt động cho tôi quá –

+0

^thử xóa setSupportActionBar() – Clocker

8

Hoạt động tốt cho tôi.

LayoutInflater mInflater=LayoutInflater.from(context); 
View mCustomView = mInflater.inflate(R.layout.toolbar_custom_view, null); 
toolbar.addView(mCustomView); 
1

Chỉ cần tăng lượt xem mà bạn muốn thêm qua chế độ xem thanh công cụ làm tham số thứ hai của phương pháp thổi phồng; Bằng cách này, lệnh gọi "addView" không cần thiết:

setSupportActionBar(toolbar); 
View logo = getLayoutInflater().inflate(R.layout.view_logo, toolbar); 
Các vấn đề liên quan