TextView绘制监听问题,记录一下

当尝试通过getLineCount()获取TextView的行数时,可能会得到0。文章提出了三种解决方案:1)使用OnPreDrawListener,2)添加OnGlobalLayoutListener,3)使用post方法。在ListView中,由于TextView的复用,前两种方案可能导致位置混乱,而第三种方案可避免此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

获得TextView的真实行数调用getLineCount()方法时,获得的始终为0 ,关于如何获得真实的行数值,有以下解决方案:

1,绘画完成之前调用的,在这里面可以获取到行数,当然也可以获取到宽高等信息。

ViewTreeObserver vto = mContent.getViewTreeObserver();

vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

@Override

public boolean onPreDraw() {

if(mContent.getLineCount() == 1){

} else {

}

return false;

}

});

2, 绘画完成之后调用的,在这里面可以获取到行数,当然也可以获取到宽高等信息。

mContent.setText(barModel.getContentTitle());

mContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

@Override

public void onGlobalLayout() {

mContent.getViewTreeObserver().removeGlobalOnLayoutListener(this);

if(mContent.getLineCount() == 1){

Log.e("TAG","---------"+mContent.getLineCount());

} else {

}

}

});

3,

使用 post,绘制完成在调用 getLineCount()

mContent.post(new Runnable() {

@Override

public void run() {

if(mContent.getLineCount() == 1){

mContent.setPadding(0,(int)mContext.getResources().getDimension(R.dimen.space_12),0,0);

mReadNum.setPadding(0,0,0,(int)mContext.getResources().getDimension(R.dimen.space_12));

mCollectionNum.setPadding(0,0,0,(int)mContext.getResources().getDimension(R.dimen.space_12));

} else {

mContent.setPadding(0,0,0,0);

mReadNum.setPadding(0,0,0,0);

mCollectionNum.setPadding(0,0,0,0);

}

}

});

经过测试三种方案均可行,但是如果是listview中TextView,根据LineCount切换位置,会出现

TextView复用,导致TextView位置在滑动速度快的情况下位置瞬间混乱,采用第三种方案没有此问题,至于采用前两种方案出现问题的原因不得而知,有遇到这种情况的可以一起探讨一下原因。

以上原文链接:https://blog.youkuaiyun.com/zhzzhz123456/article/details/52925753

在绘制前获取textView的行数可以使用StaticLayout,TextView内部实现行数计算也是使用的StaticLayout,所以new一个StaticLayout来协助计算出TextView会占用的行数,以下工具类

public class TextViewLinesUtil {

    public static int getTextViewLines(TextView textView, int textViewWidth) {
        int width = textViewWidth - textView.getCompoundPaddingLeft() - textView.getCompoundPaddingRight();
        StaticLayout staticLayout;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            staticLayout = getStaticLayout23(textView, width);
        } else {
            staticLayout = getStaticLayout(textView, width);
        }
        int lines = staticLayout.getLineCount();
        int maxLines = textView.getMaxLines();
        if (maxLines > lines) {
            return lines;
        }
        return maxLines;
    }

    /**
     * sdk>=23
     */
    @RequiresApi(api = Build.VERSION_CODES.M)
    private static StaticLayout getStaticLayout23(TextView textView, int width) {
        StaticLayout.Builder builder = StaticLayout.Builder.obtain(textView.getText(),
                0, textView.getText().length(), textView.getPaint(), width)
                .setAlignment(Layout.Alignment.ALIGN_NORMAL)
                .setTextDirection(TextDirectionHeuristics.FIRSTSTRONG_LTR)
                .setLineSpacing(textView.getLineSpacingExtra(), textView.getLineSpacingMultiplier())
                .setIncludePad(textView.getIncludeFontPadding())
                .setBreakStrategy(textView.getBreakStrategy())
                .setHyphenationFrequency(textView.getHyphenationFrequency())
                .setMaxLines(textView.getMaxLines() == -1 ? Integer.MAX_VALUE : textView.getMaxLines());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setJustificationMode(textView.getJustificationMode());
        }
        if (textView.getEllipsize() != null && textView.getKeyListener() == null) {
            builder.setEllipsize(textView.getEllipsize())
                    .setEllipsizedWidth(width);
        }
        return builder.build();
    }

    /**
     * sdk<23
     */
    private static StaticLayout getStaticLayout(TextView textView, int width) {
        return new StaticLayout(textView.getText(),
                0, textView.getText().length(),
                textView.getPaint(), width, Layout.Alignment.ALIGN_NORMAL,
                textView.getLineSpacingMultiplier(),
                textView.getLineSpacingExtra(), textView.getIncludeFontPadding(), textView.getEllipsize(),
                width);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值