如何在Android(Java)中以编程方式创建TextView多行

时间:2015-06-09 13:47:32

标签: java android textview multiline

我想让我的textview多线。

图片:http://s13.postimg.org/y0q78e1yv/Capture.png

但是如何让我的文字多行?

哪个属性?

TextView txt_tweet = (TextView) View.inflate(this, R.layout.special_textview, null);

special_textview

    <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"

    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingBottom="5dp"
    android:inputType="textMultiLine"
    android:scrollHorizontally="false">

</TextView>

8 个答案:

答案 0 :(得分:3)

您想要在同一文本视图中显示不同的文本吗?如果是这样,请使用两个文本视图,如:

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

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />
</LinearLayout>

远程android:inputType =“textMultiLine”,这是一个EditText属性。

如果您只想在同一文本视图中使用多行:

android:maxLines="5"//optional to set max numbers of lines
android:minLines="2"//optional to set min numbers of lines
android:singleLine="false"//set false to allow multiple line
android:lines="2" //or more

如果您要使用的此文本视图属于ListView,请使用:

android.R.layout.simple_list_item_2

它将为您提供两种文本视图。

答案 1 :(得分:2)

我按照以下方式做到了:

tv.setElegantTextHeight(true);
tv.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
tv.setSingleLine(false);

答案 2 :(得分:1)

你可以这样做:

txt_tweet.setSingleLine(false);
txt_tweet.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION);

答案 3 :(得分:1)

首先将“\n”替换为其等效的“&lt;br&gt;”,然后在字符串上调用Html.fromHtml()。请按照以下步骤操作:

String text= model.getMessageBody().toString().replace("\n", "&lt;br&gt;")
textView.setText(Html.fromHtml(Html.fromHtml(text).toString()))

这很有效。

答案 4 :(得分:1)

添加答案:顺序很重要!

确保在 setMinLines!

之前调用setInputType

答案 5 :(得分:0)

TextView必须将“singleLine”属性设置为false。你也应该设置'ellipsize'来包装文本:

android:singleLine="false"
android:ellipsize="end"

答案 6 :(得分:0)

布局

android:lines="8"  //Total Lines prior display
android:minLines="6" //Minimum lines
android:maxLines="10" //Maximum Lines

答案 7 :(得分:-1)

只需删除此行:

 android:inputType="textMultiLine"

EditTexts的inputType