Android: Creating a Circular TextView?

后端 未结 11 1603
我在风中等你
我在风中等你 2020-12-04 08:53

My current simple XML is below, however i would like the 3 TextViews within it to be circular, rather than rectangular.

How can I change my code to do so?

         


        
11条回答
  •  星月不相逢
    2020-12-04 09:35

    It's a rectangle that prevents oval shape background to get circular.
    Making view a square will fix everything.

    I found this solution to be clean and working for varying textsize and text length.

    public class EqualWidthHeightTextView extends TextView {
    
        public EqualWidthHeightTextView(Context context) {
            super(context);
        }
    
        public EqualWidthHeightTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public EqualWidthHeightTextView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    
            int r = Math.max(getMeasuredWidth(),getMeasuredHeight());
            setMeasuredDimension(r, r);
    
        }
    }
    


    Usage

    
    


    oval_light_blue_bg.xml

    
           android:shape="oval">
       
       
    
    

提交回复
热议问题