问题
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