【问题标题】:android - TextViews with line breakandroid - 带有换行符的 TextViews
【发布时间】:2012-10-05 21:33:18
【问题描述】:

我有 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|

【问题讨论】:

    标签: android textview android-linearlayout


    【解决方案1】:

    改变

    newTextView.setSingleLine(true);
    

     newTextView.setSingleLine(false);
    

    希望它对我的朋友有用......

    【讨论】:

      【解决方案2】:

      尝试为 textviews 设置 setEllipsize

      newTextView.setEllipsize(null);
      

      【讨论】:

        【解决方案3】:

        将 follow 设置为 false 而不是 true 以启用包装 newTextView.setSingleLine(true);

        【讨论】:

        • 你试过在你想要换行的地方插入 \n 吗?
        • 否 - 因为我不知道我想在哪里换行。我不知道字符串 p 中有什么。换行符必须位于屏幕的右端。
        【解决方案4】:

        当您在此 newTextView.setSingleLine(true); 中设置为 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-06-21
          • 1970-01-01
          • 1970-01-01
          • 2014-06-15
          • 1970-01-01
          • 2011-08-02
          • 2011-12-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多