我有很长的数据,我想在 中查看TextView
。我正在尝试使用以下代码进行分页:
TextPaint textPaint = tv.getPaint();
int boundedWidth = tv.getWidth();
StaticLayout layout = new StaticLayout(data, textPaint, boundedWidth , Alignment.ALIGN_NORMAL, 1.0f, 1, false);
layout.draw(new Canvas());
int totalLines = layout.getLineCount();
int currentPageNum = 0;
int topLine = 0;
int bottomLine = 0;
topLine = layout.getLineForVertical( currentPageNum * height );
bottomLine = layout.getLineForVertical( (currentPageNum + 1) * height );
int pageOffset = layout.getLineStart(topLine);
int pageEnd = layout.getLineEnd(bottomLine);
tv.setText(data.subSequence(pageOffset, pageEnd));
但是pageoffset
并pageend
给我startline+1
和endline+1
价值不应该绘制的文本的确切偏移量。
我的代码有什么问题?还有其他方法吗?