inflate menu in CollapsingToolbarLayout issue

元气小坏坏 提交于 2019-12-04 05:42:09

There are two ways to do it: First you need to tell the Activity that the Toolbar your using is Action Bar by using: setSupportActionBar(toolbar)

Else (I recommend):

Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
toolbar.inflateMenu(R.menu.your_menu_items);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            TODO: Write your logic here
        }
});

I believe when you say "Action icon in toolbar" you mean the UP action on the left-hand side of the toolbar.

If you mean the right-hand side menu with the 3-dots icon, @Janhavi answer is correct. If you mean the UP icon on the left-hand side, read below:

You just need to configure it on the toolbar after you inflate the layout, like the following code:

@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// inflate your view normally
View root = inflater.inflate(R.layout.my_layout, container, false);

// configure your views
Toolbar toolbar = (Toolbar)root.findViewById(R.id.toolbar);
toolbar.setNavigationIcon(R.drawable.icon);
toolbar.setNavigationOnClickListener(new View.OnClickListener(){
    @Override public void onClick(View v){
        // TODO: code your UP navigation here
        // probably: getFragmentManager().popBackStack ?
    }
});

// return the view
return root;
}

the icon you can download from here: https://www.google.com/design/icons/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!