How to set Floating Action Button image to fill the button?

后端 未结 7 1625
情歌与酒
情歌与酒 2021-01-11 17:27

Usually icons are set for floating action buttons, but I need to set an image, so I can make a circular image.

I added it to the project (using Android Studio-> New

7条回答
  •  清歌不尽
    2021-01-11 17:37

    If you are setting a rounded png image, work around for this is to extend the floatingActionButton class then override the on onMeasure method and set the padding to 0, and scaleType To centerCrop

    public class FloatingImageButton extends FloatingActionButton {
    
    
    public FloatingImageButton(Context context) {
        super(context);
    }
    
    public FloatingImageButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setPadding(0, 0, 0, 0);
        setScaleType(ScaleType.CENTER_CROP);
    
    
    }
    }
    

    xml sample

        
    

    output

提交回复
热议问题