Remove space between stacked TextViews

后端 未结 8 1784
南旧
南旧 2020-12-09 02:27

I have a vertical LinearLayout with two TextView inside it. The former contains a static text property (it\'s text never change) and the last conta

8条回答
  •  [愿得一人]
    2020-12-09 03:24

    Make baseline of the text equal to the bottom of the TextView.

    public class BaselineTextView extends TextView {
    
        public BaselineTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            int yOffset = getHeight() - getBaseline();
            canvas.translate(0, yOffset);
            super.onDraw(canvas);
        }
    
    }
    

    NOTE: To avoid chopping off descenders call setClipChildren(false) on your TextView's parent ViewGroup (android:clipChildren="false" in XML).

提交回复
热议问题