android - 带换行符的TextView

标签 android textview android-linearlayout

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

这是我的代码:

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|

最佳答案

改变

newTextView.setSingleLine(true);

 newTextView.setSingleLine(false);

希望它能起作用,我的 friend ……

关于android - 带换行符的TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12746525/

相关文章:

android - 如何使用模糊代码创建适用于 Android 的 Cordova 插件?

android - 无论如何以编程方式动画线性布局的布局权重属性

android - Android内核源代码的git repo在哪里?

android - ScrollView 中的可缩放 TextView

android - 开发人员使用哪些 Android 平板电脑来编写和测试 USB 主机应用程序?

android - 在 TextView 中通过文本画线

android - Android 中的字符串 HTML

android - LinearLayout 不填充父级

android - 如何从下往上向 LinearLayout 添加 View ?

android - LinearLayout achartengine 图表未显示在 ScrollView 中