可以在工具栏中添加 imageview 吗?
It's possible to add imageview in a toolbar?
我需要有关 Toolbar
的信息。我想在我的 Toolbar
中添加四张图片,例如主页、新闻、搜索和消息。所以我把 Imageview
s 放在 Toolbar
中。现在我不知道如何在这些图像上添加监听器……我该怎么做?
我还有一个导航抽屉,我想要它,如果你在 Toolbar
中按 Imageview
,你会进入一个新的 Fragment
而不是新的 Activity
。
那我怎么能做这两件事呢?
谢谢你的回答。
我建议使用 ImageButton
s。您可以设置将显示为 Button
的图像源,它们的行为与 OnClickListeners
的正常 Button
s 一样。
您应该只使用内置于 Toolbar
中的常规 MenuItem
功能(与 ActionBar 相同)。查找任何 ActionBar
教程,并研究使用、创建 MenuItems
(您将在 XML 中完成大部分操作)。然后,您将能够使用 OS 框架来捕获 onClickListenter
s.
这是文档(您会注意到 MenuItem
有一个图标属性,它将图像放入 Toolbar
):
android:icon="@drawable/ic_new_game"
我猜你知道如何将你的 imageView
添加到你的 ToolBar
如果不检查 this .. 之后获得对你的 ToolBar
的引用使用 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
我需要有关 Toolbar
的信息。我想在我的 Toolbar
中添加四张图片,例如主页、新闻、搜索和消息。所以我把 Imageview
s 放在 Toolbar
中。现在我不知道如何在这些图像上添加监听器……我该怎么做?
我还有一个导航抽屉,我想要它,如果你在 Toolbar
中按 Imageview
,你会进入一个新的 Fragment
而不是新的 Activity
。
那我怎么能做这两件事呢?
谢谢你的回答。
我建议使用 ImageButton
s。您可以设置将显示为 Button
的图像源,它们的行为与 OnClickListeners
的正常 Button
s 一样。
您应该只使用内置于 Toolbar
中的常规 MenuItem
功能(与 ActionBar 相同)。查找任何 ActionBar
教程,并研究使用、创建 MenuItems
(您将在 XML 中完成大部分操作)。然后,您将能够使用 OS 框架来捕获 onClickListenter
s.
这是文档(您会注意到 MenuItem
有一个图标属性,它将图像放入 Toolbar
):
android:icon="@drawable/ic_new_game"
我猜你知道如何将你的 imageView
添加到你的 ToolBar
如果不检查 this .. 之后获得对你的 ToolBar
的引用使用 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