2013-01-05 39 views
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)); 

pageoffsetpageend給我startline+1endline+1值不準確的偏移量應繪製文本。

我的代碼有什麼問題?有沒有其他方法?

回答

1

最後,我讓它工作。我的有界寬度爲零,所以它返回錯誤的數據。