Adding custom view to a toolbar

后端 未结 3 1364
庸人自扰
庸人自扰 2020-12-05 18:09

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.setCustom

相关标签:
3条回答
  • 2020-12-05 18:18

    Works great for me.

    LayoutInflater mInflater=LayoutInflater.from(context);
    View mCustomView = mInflater.inflate(R.layout.toolbar_custom_view, null);
    toolbar.addView(mCustomView);
    
    0 讨论(0)
  • 2020-12-05 18:23

    Just inflate the view that you want to add passing the toolbar view as second parameter of the inflate method; In this way the call to "addView" is not necessary:

    setSupportActionBar(toolbar);
    View logo = getLayoutInflater().inflate(R.layout.view_logo, toolbar);
    
    0 讨论(0)
  • 2020-12-05 18:35

    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.

    0 讨论(0)
提交回复
热议问题