Keep text in TextView with drawableLeft centered

后端 未结 21 1160
野的像风
野的像风 2021-02-03 17:10

In my app I have a header bar which consists of a single textview with fill_parent as width, which have a specific background color and some centered text. Now I want to add a d

21条回答
  •  北恋
    北恋 (楼主)
    2021-02-03 17:57

    Try flowing:

    public class DrawableCenterTextView extends AppCompatTextView {
    
        public DrawableCenterTextView(Context context, AttributeSet attrs,
                                      int defStyle) {
            super(context, attrs, defStyle);
        }
    
        public DrawableCenterTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public DrawableCenterTextView(Context context) {
            super(context);
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            // We want the icon and/or text grouped together and centered as a group.
            // We need to accommodate any existing padding
            final float buttonContentWidth = getWidth() - getPaddingLeft() - getPaddingRight();
    
            float textWidth = 0f;
            final Layout layout = getLayout();
            if (layout != null) {
                for (int i = 0; i < layout.getLineCount(); i++) {
                    textWidth = Math.max(textWidth, layout.getLineRight(i));
                }
            }
    
            // Compute left drawable width, if any
            Drawable[] drawables = getCompoundDrawables();
            Drawable drawableLeft = drawables[0];
    
            int drawableWidth = (drawableLeft != null) ? drawableLeft.getIntrinsicWidth() : 0;
    
            // We only count the drawable padding if there is both an icon and text
            int drawablePadding = ((textWidth > 0) && (drawableLeft != null)) ? getCompoundDrawablePadding() : 0;
    
            // Adjust contents to center
            float bodyWidth = textWidth + drawableWidth + drawablePadding;
            int translate = (int) ((buttonContentWidth - bodyWidth));
            if (translate != 0)
                setPadding(translate, 0, translate, 0);
            super.onDraw(canvas);
        }
    }
    

提交回复
热议问题