我可以在布局xml文件的TextView中设置最大行数。 在我的代码中,在将TextText()调用到TextView后,如何才能确定文本内容是否已被切断(即文本比最大行更长)?如何找出TextView文本内容是否已被切断
谢谢。
我可以在布局xml文件的TextView中设置最大行数。 在我的代码中,在将TextText()调用到TextView后,如何才能确定文本内容是否已被切断(即文本比最大行更长)?如何找出TextView文本内容是否已被切断
谢谢。
因此,虽然我没有测试此,但(对不起没有SDK安装在我的前面)
你的TextView应该有它创造了一个喷漆的对象。现在我假定TextPaint已经用正确的填充和偏移量来构建文本视图的背景图像。所以,你应该能够做到像
TextView a = getViewById(R.id.textview);
TextPaint paint = a.getPaint();
Rect rect = new Rect();
String text = String.valueOf(a.getText());
paint.getTextBounds(text, 0, text.length(), rect);
if(rect.height() > a.getHeight() || rect.getWidth() > a.getWidth()) {
Log.i("TEST", "Your text is too large");
}
我知道这是一个老问题,但我在寻找一个类似的答案碰到它,并希望提供的是我找到了解决办法,如果任何人想知道。
这为我工作:
TextView myTextView = rootView.getViewById(R.id.my_text_view);
if (myTextView.getLineCount() > myTextView.getMaxLines()) {
// your code here
}
试试这个:
mTextView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
ViewTreeObserver obs = mTextView.getViewTreeObserver();
obs.removeOnGlobalLayoutListener(this);
int height = mTextView.getHeight();
int scrollY = mTextView.getScrollY();
Layout layout = mTextView.getLayout();
int firstVisibleLineNumber = layout.getLineForVertical(scrollY);
int lastVisibleLineNumber = layout.getLineForVertical(height + scrollY);
//check is latest line fully visible
if (mTextView.getHeight() < layout.getLineBottom(lastVisibleLineNumber)) {
// TODO you text is cut
}
}
});
你是对的!你的解决方案是工作! – 2017-01-13 08:04:30
只要改变'rect.getWidth()''到rect.width()'。 – 2013-08-29 12:14:08