简体   繁体   English

形状可绘制的Android菜单图标

[英]Android menu icon from a shape drawable

Does anyone know if its possible to use an XML ShapeDrawable (or any other XML drawable which is not an image) as a menu item icon? 有谁知道是否可以使用XML ShapeDrawable(或任何其他非图像的XML drawable)作为菜单项图标? Is this not possible? 这不可能吗? and if it is, how does one go about achieving this? 如果是的话,如何实现这一目标?

TIA! TIA!

自从我完成了android开发以来,已经有些过时了,但是使用JB,您可以将其放在资源中并从应用清单中引用它(我相信就是这样吗?)

There are two ways of doing that. 有两种方法可以做到这一点。 You can change icon programmatically 您可以通过编程方式更改图标

menu.getItem(0).setIcon(getResources().getDrawable(R.drawable.ic_launcher));

or you can fix an icon in your menu.xml file 或者您可以在menu.xml文件中修复图标

android:icon="@drawable/ic_whaterver"

pskink said it in the comments (but it's easier to find as an answer). pskink在评论中表示了这一点(但更容易找到答案)。 Give the drawable a <size> tag. 给drawable一个<size>标签。

This is actually a little weird to me. 这实际上对我来说有点奇怪。 Often, the size is used only to get the aspect ratio of a <shape> , so one might assume that setting size with android:height="1" and android:width="1" would suffice. 通常,大小仅用于获取<shape>的长宽比,因此可以假设使用android:height="1"android:width="1"来设置大小就足够了。 Not so in this case - the drawable will appear with a size of 1x1 dp (probably not at all). 在这种情况下不是这样-可绘制对象的大小为1x1 dp(可能根本不显示)。

Interestingly, if you set the size to be much larger than the menu item, it is scaled down appropriately. 有趣的是,如果您将大小设置为比菜单项大得多,它将被适当缩小。 So you can set the android:height and android:width to be something outrageous like 800dp and not worry. 因此,您可以将android:heightandroid:width设置为800dp类的800dp ,不必担心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM