It's possible to add imageview in a toolbar?

主宰稳场 提交于 2019-12-07 03:24:14

问题


I need an information about Toolbar. I would like to add four images in my Toolbar like home, news, search and message. So i've putting Imageviews in the Toolbar. Now I don't know how to add listener at these images...how could I do that? I have also a navigation drawer and I want that, if you press the Imageview in the Toolbar, you go in a new Fragment and not in a new Activity. So how could I do these two things? Thank you for the answers.


回答1:


i guess you know how to add your imageViews to your ToolBar if not check this .. After that get a reference to your ToolBar use findViewById

Toolbar tool = finViewById(R.id.mytoolbar);//cast it to ToolBar
ImageView im1 = tool.findViewById(R.id.myimageviewone); // cast it
//same for the others
// now you can set your click listeners
//in your onclick listeners you use fragmentmanager along with fragmenttransaction 
//to start a new fragment in your layout or on your layout, you can google
// for that



回答2:


You should just be using the regular MenuItem functionality built into the Toolbar (and same as ActionBar). Look up any ActionBar tutorial, and look into using ,creating MenuItems (you will do most of this in XML). You will then be able to use the OS framework, to capture onClickListenters.

Here are the docs (you will notice the MenuItem has an icon attribute, which will place the images into the Toolbar):

android:icon="@drawable/ic_new_game"

Docs: http://developer.android.com/guide/topics/ui/menus.html




回答3:


I would recomend to use ImageButtons. You can set image sources which will be displayed as a Button and they behave like normal Buttons with OnClickListeners.



来源:https://stackoverflow.com/questions/30276281/its-possible-to-add-imageview-in-a-toolbar

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