Android多列文本

Ovi*_*tcu 4 android textview android-layout

有什么方法可以在多列上显示长文本吗?

例如,我需要显示一篇文章,我有整个文章,String我想把它放在3列上.我怎样才能做到这一点?有没有可以帮助我的图书馆,或者你知道我应该如何解决这个问题?

任何建议表示赞赏.谢谢.

编辑:问题是字符串的拆分而不是布局.我知道我可以使用TableLayout ...或权重...来均匀分配列等等.问题是如何String正确拆分.也许2列会被填满而第3列只有一半?我不知道如何处理这个,而不是实际的布局.

rek*_*ire 5

检查TableLayout.要分割文本,您可以在1/3的字符数之后拆分文本.对于cource,您必须将文本拆分为一个空白字符.更新:请参阅我发布结尾处的示例代码.

例

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="1">
    <TableRow>
        <TextView
            android:text="@string/table_layout_4_open"
            android:padding="3dip" />
        <TextView
            android:text="@string/table_layout_4_open_shortcut"
            android:gravity="right"
            android:padding="3dip" />
    </TableRow>

    <TableRow>
        <TextView
            android:text="@string/table_layout_4_save"
            android:padding="3dip" />
        <TextView
            android:text="@string/table_layout_4_save_shortcut"
            android:gravity="right"
            android:padding="3dip" />
    </TableRow>
</TableLayout>
Run Code Online (Sandbox Code Playgroud)

对于拆分,您可以测试此代码.algorithem可能会更接近分裂边界,但它可以工作.

public static String[] getRows(String text, int rows) {
    // some checks
    if(text==null)
        throw new NullPointerException("text was null!");
    if(rows<0 && rows > 10)
        throw new IllegalArgumentException("rows must be between 1 and 10!");
    if(rows==1)
        return new String[] { text };
    // some init stuff
    int len=text.length();
    int splitOffset=0;
    String[] ret=new String[rows];
    Pattern whitespace = Pattern.compile("\\w+");
    // do the work
    for(int row=1;row<rows;row++) {
        int end;
        int searchOffset=len/rows*row;          
        // search next white space
        Matcher matcher = whitespace.matcher(text.substring(searchOffset));
        if(matcher.find() && !matcher.hitEnd()) {
            // splitting on white space
            end=matcher.end()+searchOffset;
        } else {
            // hard splitting if there are no white spaces
            end=searchOffset;
        }
        ret[row-1]=text.substring(splitOffset, end);
        splitOffset=end;
    }
    // put the remaing into the last element
    ret[rows-1]=text.substring(splitOffset);
    return ret;
}
Run Code Online (Sandbox Code Playgroud)