Android中的多列文本显示

时间:2013-05-01 07:26:39

标签: android android-layout

我想在2栏中显示文字。

实施例。我有大字符串,第一列添加第一列,屏幕高度结束后,应在第二列添加文本。就像新闻报道一样。

我试过下面的代码,但它给了我 java.lang.StringIndexOutOfBoundsException

java代码

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>

3 个答案:

答案 0 :(得分:2)

我刚创建了一个示例项目,该文档将文本呈现为多列:https://github.com/viht0ri/ColumnLayout

这种方法使用android / text / Layout类来格式化和绘制文本。

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);
}

答案 1 :(得分:0)

在尝试从文本

获取子字符串之前,确保totalText变量小于text.length()

答案 2 :(得分:0)

您可以使用表行并在其中插入TextView。然后将字符串分成两部分,并将它们放在两个相邻的textview中。

<TableRow>

   <TextView
        android:text=""
        android:id="+id/col1"
        android:padding="10dip" />

    <TextView
        android:text=""
        android:id="+id/col2"
        android:gravity="right"
        android:padding="10dip" />

</TableRow>

将字符串分成两部分后,将它们放在TableLayout中的两个TextView中。

这样可行。