添加自定义视图工具栏 [英] Adding custom view to a toolbar

查看:384
本文介绍了添加自定义视图工具栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自定义视图添加到新的工具栏(棒棒堂)。但不知何故,认为被添加的工具栏的下方。这是工作正常,当我使用actionBar.setCustomView但现在迁移到工具栏后,这是行不通的。下面是code。应做什么样的变化?

片段:

 工具栏=(栏)getView()findViewById(R.id.toolbar)。
    ((ActionBarActivity)getActivity())setSupportActionBar(工具栏)。

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



    动作条动作条=((ActionBarActivity)getActivity())
            .getSupportActionBar();

    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

    LayoutInflater充气=(LayoutInflater)getActivity()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    //膨胀的观点
    最终的视图中查看= inflater.inflate(R.layout.actionbar_search,NULL);
    最后ImageView的searchIcon =(ImageView的)查看
            .findViewById(R.id.search_icon);
    最后ClearableAutoCompleteTextView搜索盒=(ClearableAutoCompleteTextView)查看
            .findViewById(R.id.search_box);

    //启动与文本视图中隐藏在操作栏
    sea​​rchBox.setVisibility(View.INVISIBLE);
    sea​​rchIcon.setOnClickListener(新View.OnClickListener(){

        @覆盖
        公共无效的onClick(视图v){
            toggleSearch(假,视图);
        }
    });

    sea​​rchBox.setOnClearListener(新OnClearListener(){

        @覆盖
        公共无效onClear(){
            toggleSearch(真,查看);
        }
    });

    sea​​rchBox.setOnItemClickListener(新AdapterView.OnItemClickListener(){

        @覆盖
        公共无效onItemClick(适配器视图<>母公司视图中查看,
                INT位置,长的id){


        }

    });

toolbar.addView(视图);
// actionBar.setCustomView(视图); //这个工作previously
//((ActionBarActivity)getActivity()).getSupportActionBar().setCustomView(view); //与工具栏不工作
 

解决方案

通过工具栏我已经成功地做​​到这一点是这样的:

  setSupportActionBar(工具栏);
    查看标志= getLayoutInflater()膨胀(R.layout.view_logo,空)。
    toolbar.addView(标识);
 

或者你也可以只是你的看法添加到工具栏的XML,因为它只是一个ViewGroup中。这样,您将有preVIEW在布局编辑器。没有Java code必需的。

I am trying to add a custom view to the new toolbar (Lollipop) . But somehow the view gets added below the toolbar. It was working fine when I used actionBar.setCustomView but now after migrating to toolbar, it doesn't work. Below is the code. What changes should be done?

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

解决方案

With toolbar I've managed to achieve that like this:

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

Or you can also just add your view to the toolbar xml, as it's just a ViewGroup. This way you'll have the preview in layout editor. No java code required.

这篇关于添加自定义视图工具栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆