简体   繁体   中英

How to set size of context menu header icon?

How can I set the size of the icon in the header of a context menu?

在此处输入图片说明

The icon is set via ContextMenu.setHeaderIcon(int iconRes) .

Just in case someone else faces the same problem, I have eventually implemented it the following way.

I have written a generic method which takes a Drawable and the desired width and height as parameters. It returns a scaled (Bitmap)Drawable based on the given specs.

public Drawable getScaledIcon( Drawable drawable, int dstWidth, int dstHeight ) {

    Bitmap bitmap = ( (BitmapDrawable) drawable ).getBitmap();
    Bitmap bitmapScaled = Bitmap.createScaledBitmap( bitmap, dstWidth, dstHeight, false );

    return new BitmapDrawable( getResources(), bitmapScaled );
}

The method for setting the context menu header icon takes as a parameter either a resource ID via ContextMenu.setHeaderIcon(int iconRes) or a Drawable via ContextMenu.setHeaderIcon(Drawable icon) .

Use the latter in conjunction with getScaledIcon(...) and you're done! :)

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