How to remove the top and bottom space on textview of Android

前端 未结 14 1992
执笔经年
执笔经年 2020-11-28 04:58

When I include the below XML to layout file, I can see the below image. If you see it, you could realize that the TextView has top and bot

14条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-28 05:19

    This is the code that saved our day. It was adapted using mono C# code from maksimko:

    public class TopAlignedTextView extends TextView {
    
        public TopAlignedTextView(Context context) {
            super(context);
        }
    
        /*This is where the magic happens*/
        @Override
        protected void onDraw(Canvas canvas){
    
            float offset = getTextSize() - getLineHeight();
            canvas.translate(0, offset);
            super.onDraw(canvas);
        }
    }
    

    Still had to play around with textView.setIncludeFontPadding(false) because we were aligning TextViews with different font sizes.

提交回复
热议问题