1
我有很長的數據,我想在TextView
中看到。我想用下面的代碼上分頁工作:如何在Android 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
值不準確的偏移量應繪製文本。
我的代碼有什麼問題?有沒有其他方法?