Individual line spacing for each line

前端 未结 5 1291
感动是毒
感动是毒 2020-12-29 12:36

Is it possible to define individual line spacings for each text line of a TextView?

Example:

TextView tv = new TextView(context);
tv.set         


        
5条回答
  •  甜味超标
    2020-12-29 13:19

    Android 10 (API 29) added LineHeightSpan.Standard to do set an absolute line height. If you need backward compatibility, use the following implementation which I based on LineHeightSpan.Standard:

    private static class AbsoluteHeightSpan implements LineHeightSpan {
        private final int _height;
    
        AbsoluteHeightSpan(int height) {
            _height = height;
        }
    
        @Override
        public void chooseHeight(CharSequence text, int start, int end, int spanstartv, int v, FontMetricsInt fm) {
            final int originHeight = fm.descent - fm.ascent;
            // If original height is not positive, do nothing
            if (originHeight <= 0)
                return;
            final float ratio = _height * 1.0f / originHeight;
            fm.descent = Math.round(fm.descent * ratio);
            fm.ascent = fm.descent - _height;
        }
    }
    

提交回复
热议问题