How to get linespacing of a TextView?

泄露秘密 提交于 2019-12-23 16:56:49

问题


Is there any way to get the linespacing of a TextView in Android? I try to find fontMetrics of the Paint of the TextView and do like this:

tv1.getPaint().getFontMetrics(pfm);
float fontTop = pfm.top;
float fontBottom = pfm.bottom;
System.out.println("Line Space >> " + (fontBottom - fontTop));

But it seems that result is the same until I change font size of the TextView. So how can I get the linespacing of a TextView?


回答1:


Linespacing of TextView is

textView.getPaint().getFontSpacing() * textView.getLineSpacingMultiplier() + textView.getLineSpacingExtra();

Note getLineSpacingMultiplier() and getLineSpacingExtra() methods are available since API 16+.




回答2:


getLineSpacingExtra (); Gets the line spacing extra space

tv1.getLineSpacingExtra ()

textView.setLineSpacing() or from xml you can use android:lineSpacingExtra for setting the extra space.




回答3:


    try {
        Field mSpacingAddField = TextView.class.getDeclaredField("mSpacingAdd");
        Field mSpacingMultField = TextView.class.getDeclaredField("mSpacingMult");
        mSpacingAddField.setAccessible(true);
        mSpacingMultField.setAccessible(true);
        mSpacingAdd = mSpacingAddField.getFloat(textView);
        mSpacingMult = mSpacingMultField.getFloat(textView);
    } catch (Exception e) {
        e.printStackTrace();
    }


来源:https://stackoverflow.com/questions/22243801/how-to-get-linespacing-of-a-textview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!