Android中多列文本显示 [英] Multi-Column Text Displays in Android
问题描述
我想在2列中显示的文本。
防爆。我有大串,第一串中的第一列中添加和获取屏幕高度的结束后,文应加上第二列。如新闻篾。
我有尝试以下code,但它给我的 java.lang.StringIndexOutOfBoundsException
Java的code
最后的TextView TVL =(的TextView)findViewById(R.id.textl);
最终的TextView TVR =(的TextView)findViewById(R.id.textr);
最后字符串文本=自卫队
tvl.post(新的Runnable(){
@覆盖
公共无效的run(){
TextMeasure(文字,TVL,TVR);
}
});
私人无效TextMeasure(字符串文本,
TextView的电视线,TextView的TVR){
//获取文本行数,将适合在屏幕上
INT linesPerScreen = tvl.getHeight()/(tvl.getLineHeight());
//测量有多少文本将在TextView的契合
涂料粉刷= tvl.getPaint();
INT输出textWidth = paint.breakText(文字,0,text.length()
真的,tvl.getWidth(),NULL);
//文字总量填补了TextView的是
//大约是:
INT totalText =输出textWidth * linesPerScreen;
串leftText = text.substring(0,totalText);
字符串rightText = text.substring(totalText,
text.length());
tvl.setText(leftText);
tvr.setText(rightText);
}
XML
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=横向
机器人:weightSum =2
工具:上下文=MainActivity。> <的TextView
机器人:ID =@ + ID / textl
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:layout_weight =1
机器人:文字=@字符串/参考hello world/>
<的TextView
机器人:ID =@ + ID / textr
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:layout_weight =1
机器人:文字=@字符串/参考hello world/>< / LinearLayout中>
我刚刚创建这使得文本的多个列的示例项目:的 https://github.com/viht0ri/ColumnLayout
此方法采用了Android /文字/布局类格式化和绘制文本。
保护无效的onDraw(帆布油画){
如果(mTextLayoutNeeded){
createLayouts(的getWidth(),的getHeight());
}
super.onDraw(画布);
canvas.save();
canvas.translate(getPaddingLeft(),getPaddingTop());
对于(布局L:布局){
l.draw(画布);
canvas.translate(mColumnWidth,0);
canvas.translate(mSpacing,0);
}
canvas.restore();
}私人无效createLayouts(INT宽度,高度INT){
layouts.clear();
如果(MTEXT == NULL){
返回;
}
INT availableWidth =的getWidth() - getPaddingLeft() - getPaddingRight();
INT availableHeight =的getHeight() - getPaddingTop() - getPaddingBottom();
布局masterLayout = createLayout(mColumnWidth,MTEXT,mPaint);
INT STARTLINE = 0;
INT usedWidth = 0;
而(usedWidth< availableWidth - mSpacing - mColumnWidth){
INT startLineTop = masterLayout.getLineTop(STARTLINE);
INT底线= STARTLINE;
的for(int i = STARTLINE; I< masterLayout.getLineCount();我++){
如果(masterLayout.getLineBottom(ⅰ) - startLineTop&下; availableHeight){
底线= I;
}否则如果(==底线STARTLINE){
//一个大的图像可以比现有的高度越大,跳过内容
Toast.makeText(的getContext(),跳过过大内容,Toast.LENGTH_SHORT).show();
STARTLINE ++;
startLineTop = masterLayout.getLineTop(STARTLINE);
底线= STARTLINE;
}其他{
打破;
}
}
INT columnStart = masterLayout.getLineStart(STARTLINE);
INT columnEnd = masterLayout.getLineEnd(底线);
layouts.add(createLayout(mColumnWidth,MTEXT,columnStart,columnEnd,mPaint));
如果(底线== masterLayout.getLineCount() - 1){
打破;
}
usedWidth + = mColumnWidth;
STARTLINE =底线;
}
mTextLayoutNeeded = FALSE;
}私有静态布局createLayout(INT宽度,文字的CharSequence,TextPaint漆){
返回新StaticLayout(文字,0,text.length(),涂料,
宽度,Layout.Alignment.ALIGN_NORMAL,1F,0,真正的);
}私有静态布局createLayout(INT宽度,文字的CharSequence,诠释抵消,诠释年底,TextPaint漆){
返回新StaticLayout(文字,抵消,最终,油漆,
宽度,Layout.Alignment.ALIGN_NORMAL,1F,0,真正的);
}
I want to display text in 2 column.
Ex. I have large String , First string add in first column and after getting end of screen height , text should be add on second column. like news papaer.
I have try below code , but its give me java.lang.StringIndexOutOfBoundsException
java code
final TextView tvl = (TextView)findViewById(R.id.textl);
final TextView tvr = (TextView)findViewById(R.id.textr);
final String text = "sdf";
tvl.post(new Runnable() {
@Override
public void run() {
TextMeasure(text,tvl,tvr);
}
});
private void TextMeasure(String text,
TextView tvl,TextView tvr) {
// Get number of lines of text that will fit on the screen
int linesPerScreen = tvl.getHeight()/(tvl.getLineHeight() );
// Measure how much text will fit across the TextView
Paint paint = tvl.getPaint();
int textWidth = paint.breakText(text, 0, text.length(),
true, tvl.getWidth(), null);
// Total amount of text to fill the TextView is
// approximately:
int totalText = textWidth * linesPerScreen;
String leftText = text.substring(0,totalText);
String rightText = text.substring(totalText,
text.length());
tvl.setText(leftText);
tvr.setText(rightText);
}
xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/hello_world" />
<TextView
android:id="@+id/textr"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/hello_world" />
</LinearLayout>
I just created an example project which renders text to multiple columns: https://github.com/viht0ri/ColumnLayout
This approach uses android/text/Layout class to format and draw the text.
protected void onDraw(Canvas canvas) {
if(mTextLayoutNeeded) {
createLayouts(getWidth(), getHeight());
}
super.onDraw(canvas);
canvas.save();
canvas.translate(getPaddingLeft(), getPaddingTop());
for(Layout l : layouts) {
l.draw(canvas);
canvas.translate(mColumnWidth, 0);
canvas.translate(mSpacing, 0);
}
canvas.restore();
}
private void createLayouts(int width, int height) {
layouts.clear();
if(mText == null) {
return;
}
int availableWidth = getWidth() - getPaddingLeft() - getPaddingRight();
int availableHeight = getHeight() - getPaddingTop() - getPaddingBottom();
Layout masterLayout = createLayout(mColumnWidth, mText, mPaint);
int startLine = 0;
int usedWidth = 0;
while(usedWidth < availableWidth - mSpacing - mColumnWidth) {
int startLineTop = masterLayout.getLineTop(startLine);
int endLine = startLine;
for(int i = startLine; i < masterLayout.getLineCount(); i++) {
if(masterLayout.getLineBottom(i) - startLineTop < availableHeight) {
endLine = i;
} else if(endLine == startLine) {
//A large image can be larger than the available height, skip the content
Toast.makeText(getContext(), "Skipping too large content", Toast.LENGTH_SHORT).show();
startLine++;
startLineTop = masterLayout.getLineTop(startLine);
endLine = startLine;
} else {
break;
}
}
int columnStart = masterLayout.getLineStart(startLine);
int columnEnd = masterLayout.getLineEnd(endLine);
layouts.add(createLayout(mColumnWidth, mText, columnStart, columnEnd, mPaint));
if(endLine == masterLayout.getLineCount() - 1) {
break;
}
usedWidth += mColumnWidth;
startLine = endLine;
}
mTextLayoutNeeded = false;
}
private static Layout createLayout(int width, CharSequence text, TextPaint paint) {
return new StaticLayout(text, 0, text.length(), paint,
width, Layout.Alignment.ALIGN_NORMAL, 1f, 0, true);
}
private static Layout createLayout(int width, CharSequence text, int offset, int end, TextPaint paint) {
return new StaticLayout(text, offset, end, paint,
width, Layout.Alignment.ALIGN_NORMAL, 1f, 0, true);
}
这篇关于Android中多列文本显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!