zoukankan      html  css  js  c++  java
  • 去除TextView设置lineSpacingExtra后,最后一行多出的空白

    转载请标明出处:https://www.cnblogs.com/tangZH/p/11985745.html

    更多精彩文章:http://77blogs.com/?p=278

    有些手机中,给TextView设置lineSpacingExtra后会出现最后一行的文字也出现lineSpacingExtra,不是某些版本才会,这跟机型有关。

    可以用下面这种方法解决:

    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;
        }
    }
  • 相关阅读:
    VSCode集成TypeScript编译
    http模拟登陆及发请求
    1​1​.​0​5​9​2​M​晶​振​与12M晶振
    单片机定时器2使用
    Altium Designer 小记
    sql-mysql
    java英文缩写
    Altium Design
    Tomcat使用
    jar/war/ear文件的区别
  • 原文地址:https://www.cnblogs.com/tangZH/p/11985745.html
Copyright © 2011-2022 走看看