在android中添加上下文菜单图标
我有一个带有 ContextMenu 的 Listview,但是当我为 ContextMenu setIcon 时,它看起来不起作用
public void onCreateContextMenu(ContextMenu menu , View v,
ContextMenuInfo menuInfo){
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, DELETE_ID, 0, R.string.context_menu_favorite)
.setIcon(android.R.drawable.btn_star);
}
I have a Listview with a ContextMenu, but when I setIcon for ContextMenu look like it doesn't work
public void onCreateContextMenu(ContextMenu menu , View v,
ContextMenuInfo menuInfo){
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, DELETE_ID, 0, R.string.context_menu_favorite)
.setIcon(android.R.drawable.btn_star);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这可能是您所缺少的:
This might be what you are missing:
虽然 API 不支持上下文菜单中的图标,但我们总是可以通过使用看起来像上下文菜单的我们自己的视图来膨胀对话框来伪造它。
复制粘贴以下文件即可完成这项工作:
MainActivity.java
ContextMenuItem.java
ContextMenuAdapter.java
context_menu_item.xml
listview_context_menu.xml
While the API doesn't support icons in Context Menu, but we can always fake it by inflating a Dialog with our own view that looks like context menu.
Copy-pasting the following files exactly will do the job:
MainActivity.java
ContextMenuItem.java
ContextMenuAdapter.java
context_menu_item.xml
listview_context_menu.xml
虽然 Android API 不允许在上下文菜单中使用图标,但您可以看到 Android 在很多地方都在使用它们。 长按主屏幕就是一个很好的例子。
我花时间深入研究 Launcher 和 AnyCut 源代码,发现 Google 正在使用他们自己的自定义类,该类扩展了 BaseAdapter 以及他们自己的自定义布局。
我能够几乎完全复制他们的类和布局,并在我自己的应用程序中使用它来完成。 如果您要搜索该类,则该类名为 AddAdapter.java。
享受!
While the Android API does not allow for Icons in Context menus you can see many places where Android is using them. Long pressing your home screen is one good example.
I took the time to dig through the Launcher and AnyCut source and found that Google is using their own custom class that extends a BaseAdapter along with their own custom layout.
I was able to copy their class and layout almost exactly and use it in my own app to accomplish. The class if you want to search for it is called AddAdapter.java.
Enjoy!
我是通过这种方式做到的:
参考截图:
菜单:
menu_病人_语言.xml
样式:
style.xml:
Java代码:
希望这对您有所帮助。
完成
I did it by this way:
Reference screenshot:
Menu:
menu_patient_language.xml
Style:
style.xml:
Java code:
Hope this would help you sure.
Done
该库允许您使用标准 XML 菜单创建带有图标的上下文菜单(实现为
AlertDialog
)。https://code.google.com/p/android-icon-context-菜单/
This library allows you to have a context menu (implemented as
AlertDialog
) with icons using a standard XML menu.https://code.google.com/p/android-icon-context-menu/
上下文菜单不支持图标。
Context menus do not support icons.