1

我想在 2 列中显示文本。

前任。我有大字符串,第一列添加第一个字符串,在屏幕高度结束后,文本应该添加到第二列。像新闻纸。

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

爪哇代码

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>
4

3 回答 3

3

我刚刚创建了一个将文本呈现到多列的示例项目: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);
}
于 2014-09-04T15:35:32.150 回答
0

您可以使用 Table Row 并在其中插入 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 中。

这会奏效。

于 2013-05-01T08:06:26.340 回答
0

在尝试从文本中获取子字符串之前,请确保 totalText 变量小于 text.length()

于 2013-05-01T07:36:24.317 回答