android - Android 中的多列文本显示

标签 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>

最佳答案

我刚刚创建了一个将文本呈现到多列的示例项目: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);
}

关于android - Android 中的多列文本显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16314312/

相关文章:

java - Android TranslateAnimation - 将图像从屏幕右侧移动到屏幕中

android - 线性布局 - weight 和 FILL_PARENT 的区别

android - Cordova Build Android 失败,退出代码为 ENOENT

Android开发使用加工草图

java - 低质量图像android开发

android - 如何首先解析 "The specified child already has a parent. You must call removeView() on the child' 的父级。”?

android - 启动另一个应用程序,然后返回上一个应用程序,它会切换到下一页

android - eglCodecCommon : setVertexArrayObject: set vao debug message

android - 强制 ScrollView 和/或 RecyclerViewt 始终可滚动

java - setContentView() 与 GLSurfaceView 一起使用时崩溃