2

我有 TextViews,我希望它们像换行一样成行。TextViews 是具有某些功能的对象。它们是由逗号分隔的数组中的单词。我想用换行符按顺序显示它。

这是我的代码:

int i;
String p = "one, two, three, four, five, six, seven, eight, ten";
String[] array = p.split(",");

LinearLayout groupLL = new LinearLayout(this);
LinearLayout.LayoutParams gLLP = new LinearLayout.LayoutParams(
    new ViewGroup.MarginLayoutParams(
    LinearLayout.LayoutParams.WRAP_CONTENT,
    LinearLayout.LayoutParams.WRAP_CONTENT));

LinearLayout.LayoutParams mlp = new LinearLayout.LayoutParams(
    new ViewGroup.MarginLayoutParams(
    LinearLayout.LayoutParams.FILL_PARENT,
    LinearLayout.LayoutParams.WRAP_CONTENT));
mlp.setMargins(0, 0, 10, 0);

for (i = 0; i < array.length; i++) {
    TextView newTextView = new TextView(this);
    newTextView.setText(array[i]);
    newTextView.setBackgroundColor(Color.RED);
    newTextView.setSingleLine(true);
    newTextView.setTextSize(20);

    groupLL.addView(newTextView, mlp);
}

ll.addView(groupLL, gLLP);

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/ScrollView02"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

    <RelativeLayout
    android:id="@+id/RelativeLayout02"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout android:id="@+id/LinearLayout2"
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical" >
    </LinearLayout>

    </RelativeLayout>

</ScrollView>

我有这个:

|one two three four five s|
|                         |
|                         |

我要这个:

|one two three four five  |
|six seven eight ten      |
|                         |

编辑:

如果我改变 newTextView.setSingleLine(true); 到 newTextView.setSingleLine(false); 然后我有这个:

|one two three four five six |
|                         sev|
|                          en|
4

4 回答 4

1

改变

newTextView.setSingleLine(true);

 newTextView.setSingleLine(false);

希望它会工作我的朋友......

于 2012-10-05T12:48:24.237 回答
1

尝试为 textviews 设置 setEllipsize

newTextView.setEllipsize(null);
于 2012-10-05T12:49:03.790 回答
0

将 following 设置为 false 而不是 true 以启用换行newTextView.setSingleLine(true);

于 2012-10-05T12:48:13.220 回答
0

当您在此设置 true 时,newTextView.setSingleLine(true);如果您希望文本在下一行换行,则需要设置为 false

只需newTextView.setSingleLine(false);在 for 循环中更改它

根据您的代码,您每次都在新的 TextView 中添加数组对象的单个元素,并且不需要为此设置此方法,您需要为此更改布局。如果您要在单个 textview 中附加或设置 p 字符串,则可以根据需要获得,但是使用多个 textview,则需要更改布局。

对于单个文本视图

TextView newTextView = new TextView(this);
newTextView.setText(p);
newTextView.setBackgroundColor(Color.RED);
newTextView.setTextSize(20);

groupLL.addView(newTextView, mlp);
于 2012-10-05T12:47:17.397 回答