How to have Image and Text Center within a Button

后端 未结 12 2223
感动是毒
感动是毒 2020-12-02 15:48

I want to display TEXT and Icon on a Button.

+----------------------------+
|          Icon TEXT         |
+---------------------         


        
12条回答
  •  借酒劲吻你
    2020-12-02 16:06

    Similar to some other approaches, I think a good solution is to extend Button and add the missing functionality by overriding its onLayout method:

    public class CenteredIconButton extends Button {
        private static final int LEFT = 0, TOP = 1, RIGHT = 2, BOTTOM = 3;
    
        // Pre-allocate objects for layout measuring
        private Rect textBounds = new Rect();
        private Rect drawableBounds = new Rect();
    
        public CenteredIconButton(Context context) {
            this(context, null);
        }
    
        public CenteredIconButton(Context context, AttributeSet attrs) {
            this(context, attrs, android.R.attr.buttonStyle);
        }
    
        public CenteredIconButton(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
            super.onLayout(changed, left, top, right, bottom);
    
            if (!changed) return;
    
            final CharSequence text = getText();
            if (!TextUtils.isEmpty(text)) {
                TextPaint textPaint = getPaint();
                textPaint.getTextBounds(text.toString(), 0, text.length(), textBounds);
            } else {
                textBounds.setEmpty();
            }
    
            final int width = getWidth() - (getPaddingLeft() + getPaddingRight());
    
            final Drawable[] drawables = getCompoundDrawables();
    
            if (drawables[LEFT] != null) {
                drawables[LEFT].copyBounds(drawableBounds);
                int leftOffset =
                        (width - (textBounds.width() + drawableBounds.width()) + getRightPaddingOffset()) / 2 - getCompoundDrawablePadding();
                drawableBounds.offset(leftOffset, 0);
                drawables[LEFT].setBounds(drawableBounds);
            }
    
            if (drawables[RIGHT] != null) {
                drawables[RIGHT].copyBounds(drawableBounds);
                int rightOffset =
                        ((textBounds.width() + drawableBounds.width()) - width + getLeftPaddingOffset()) / 2 + getCompoundDrawablePadding();
                drawableBounds.offset(rightOffset, 0);
                drawables[RIGHT].setBounds(drawableBounds);
            }
        }
    }
    

    The sample only works for left and right drawables, but could be extended to adjust top and bottom drawables too.

提交回复
热议问题