当前位置: 移动技术网 > 移动技术>移动开发>Android > 去除TextView设置lineSpacingExtra后,最后一行多出的空白

去除TextView设置lineSpacingExtra后,最后一行多出的空白

2019年12月05日  | 移动技术网移动技术  | 我要评论

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

有些手机中,给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;
    }
}

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网