Android TextView 设置行间距的问题 (setLineSpacing ,lineSpacingExtra, lineSpacingMultiplier)

最近的项目中,需要用到自定义控件。有个组件要获取绘制文本的高度,翻开TextView 的源码,有个方法 getLineHeight()

    public int getLineHeight() {
        return FastMath.round(mTextPaint.getFontMetricsInt(null) * mSpacingMult + mSpacingAdd);
    }
getLineHeight 的注释如下:

/**
* @return the height of one standard line in pixels. Note that markup * within the text can cause individual lines to be taller or shorter * than this height, and the layout may contain additional first- * or last-line padding. */ 返回一行的高度。注意标记内的文本可能高于或低于这个高度,布局可能包含额外的第一行或最后一行填充的部分。
我们只是获取一行文字的高度,不计算间距。
所以使用 FastMath.round( mTextPaint.getFontMetricsInt(null) ) 即可。

在网上也找到了一些通过其他办法,获取文本高度方式。在测试之后,发现使用上述方法,取到的高度最精确。测试的代码就不贴了。。

 

 

posted on 2014-06-26 15:29  闫亚迪  阅读(8853)  评论(0编辑  收藏  举报

导航