在 Honeycomb Android 3.0 中显示 Action Bar 菜单项的图标
我正在使用 Honeycomb android 3.0 开发 Android 应用程序。我正在尝试在 Action Bar 中显示菜单。菜单有一个图标和标题。当我们单击菜单项时,它会以下拉列表的形式显示其项目。它是下拉列表中带有项目名称但不显示图标的项目。
我希望在单击菜单时出现的下拉列表中的标题旁边显示一个图标。谁能帮我解决这个问题。我的 XML 文件如下:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/addserver"
android:icon="@android:drawable/ic_menu_add"
android:title="Add Server"
android:showAsAction="ifRoom|withText"
>
<menu>
<item android:id="@+id/fileserver"
android:icon="@android:drawable/ic_menu_add"
android:title="File Server"
android:onClick="onCreate"
android:showAsAction="always"/>
<item android:id="@+id/sharepoint"
android:icon="@android:drawable/ic_menu_add"
android:title="Share Point"
android:onClick="onCreate" />
</menu>
</item>
最初它显示添加服务器,左侧带有图标。单击它将显示文件服务器、共享点作为没有图标的下拉列表,尽管我给出了 android:icon 语句。
我该如何解决这个问题?
I am developing an Android application using Honeycomb android 3.0 . I am trying to display a menu in Action Bar . The menu has an icon and title. When we click the menu item it displays its items in the form of a dropdown list. It was the items in drop down list with item names but without icon it is displaying.
I want an icon to be displayed beside the title in drop down list which appears when I click the menu. can anyone help me in sorting out this issue. My XML file is as below:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/addserver"
android:icon="@android:drawable/ic_menu_add"
android:title="Add Server"
android:showAsAction="ifRoom|withText"
>
<menu>
<item android:id="@+id/fileserver"
android:icon="@android:drawable/ic_menu_add"
android:title="File Server"
android:onClick="onCreate"
android:showAsAction="always"/>
<item android:id="@+id/sharepoint"
android:icon="@android:drawable/ic_menu_add"
android:title="Share Point"
android:onClick="onCreate" />
</menu>
</item>
Initially it displays add server with icon on left. clicking on that will display fileserver, sharepoint as dropdown list without icon though I given android:icon statement.
How can I sort out this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
操作栏的溢出菜单中不显示图标的行为是设计使然(截至撰写本文时)。如果您绝对需要使用图标,
您需要编写自定义实现,并考虑重新考虑您的设计以适应 UI 约定。The behavior where icons are not displayed in the action bar's overflow menu is by design (as of this writing). If you absolutely need to use icons,
you'll need to write a custom implementationconsider rethinking your design to fit the UI conventions.实际上,有一种方法可以将图标放在菜单项的文本旁边:
这会将图标放在菜单项的开头,就在其原始文本之前。
顺便说一句,这也适用于 PopupMenu。
Actually, there is a way to put icons next to the texts for the menu items:
This will put an icon at the beginning of the menu item, right before its original text.
BTW, this will also work on PopupMenu.
尽管最初的问题有点老了,而且,因为反对在菜单中显示图标的推理有点缺乏实质内容(请参阅 Steven Elliott 的精彩评论 在 Honeycomb android 3.0 中显示操作栏菜单项的图标 ),我想指出这里给出的一个很棒的、有效的解决方案:
只需将此代码添加到您的活动中并导入适当的模块。再说一次,这不是我的工作,但仍然在工作。
Though the original question is a bit old and moreover, because reasoning against showing icons in the menu is somewhat lacking substance (see Steven Elliott's excellent remark Displaying icon for menu items of Action Bar in Honeycomb android 3.0), I'd like to point to a great, working solution that was given here:
Simply add this code to your activity and import the appropriate modules. Again, not my work, but working none the less.