How to remove TextView top margin?

后端 未结 4 2011
日久生厌
日久生厌 2021-01-31 04:19

I do have a problem with TextView. I don\'t want to have any margin/padding above it.



        
4条回答
  •  误落风尘
    2021-01-31 04:51

    I had the same issue where setting android:includeFontPadding=false did not help. The best solution I could find in reasonable time was to override the TextView's onDraw method and to adjust the canvas for the difference between the font metrics' top and ascent values:

    FontMetricsInt fontMetricsInt;
    @Override
    protected void onDraw(Canvas canvas) {
        if (adjustTopForAscent){
            if (fontMetricsInt == null){
                fontMetricsInt = new FontMetricsInt();
                getPaint().getFontMetricsInt(fontMetricsInt);
            }
            canvas.translate(0, fontMetricsInt.top - fontMetricsInt.ascent);
        }
        super.onDraw(canvas);
    }
    

提交回复
热议问题