转载请标明出处:http://77blogs.com/?p=278
有些手机中,给TextView设置lineSpacingExtra后会出现最后一行的文字也出现lineSpacingExtra,不是某些版本才会,这跟机型有关。
可以用下面这种方法解决:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
public class LastLineNoSpaceTextView extends AppCompatTextView { private Rect mRect; public LastLineNoSpaceTextView(Context context) { super(context); init(); } public LastLineNoSpaceTextView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public LastLineNoSpaceTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { mRect = new Rect(); } protected void onMeasure(int i, int i2) { super.onMeasure(i, i2); int measuredHeight = getMeasuredHeight() - getLastLineSpace(); setMeasuredDimension(getMeasuredWidth(), measuredHeight); } public int getLastLineSpace() { int lastLineIndex = getLineCount() - 1; if (lastLineIndex < 0) { return 0; } Layout layout = getLayout(); int baseline = getLineBounds(lastLineIndex, mRect); if (getMeasuredHeight() - getPaddingTop() - getPaddingBottom() != layout.getHeight()) { return 0; } int fontHeight = baseline + layout.getPaint().getFontMetricsInt().descent; int lineHeight = mRect.bottom; return lineHeight - fontHeight; } } |