for循环下的textView getLineCount

时间:2016-02-12 02:40:02

标签: android textview

我想在设置多个文本时计算行数,并在有超过3行时执行操作。

这是我到目前为止所尝试的......

private void addComment(String comment){
tComment.setText(comment);
tComment.getViewTreeObserver().addOnGlobalLayoutListener(new
                    ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Log.d("Comment is-->",tComment.getText().toString());
            lineCnt = tComment.getLineCount();
            if (lineCnt > 3) {
                Log.d("Comment","up to 3 -->"+lineCnt);
                tComment.setSingleLine(false);
                tComment.setEllipsize(TextUtils.TruncateAt.END);
                tComment.setLines(3);

            }else{
                Log.d("Comment"," -->"+lineCnt);
            }
        }
    });
}

我多次调用此addComment方法(在for循环中) 它只给了我最后一个文字的计数。

请考虑提供任何有用的方式,谢谢

0 个答案:

没有答案