简体   繁体   中英

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? Is this not possible? and if it is, how does one go about achieving this?

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

android:icon="@drawable/ic_whaterver"

pskink said it in the comments (but it's easier to find as an answer). Give the drawable a <size> tag.

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. Not so in this case - the drawable will appear with a size of 1x1 dp (probably not at all).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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