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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30733929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 10:05:30  来源:igfitidea点击:

How to make a TextView multiline programmatically in Android (Java)

javaandroidtextviewmultiline

提问by mistermm

I want to make my textview multiline.

我想让我的 textview 多行。

Image : http://s13.postimg.org/y0q78e1yv/Capture.png

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

But how can I make my text multiline ?

但是我怎样才能使我的文本多行?

Which atribut ?

哪个属性?

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

special_textview

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>

采纳答案by Kamila Brito

You want to show to different texts in the same textview? if so, use two text views like:

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

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

Remote android:inputType="textMultiLine", this is an EditText Atribute.

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

If you just want to use more then one line in the same text view:

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

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

If this textview you want to use belongs to a ListView, just use:

如果您要使用的此 textview 属于 ListView,只需使用:

android.R.layout.simple_list_item_2

It will give you two texts views to work on.

它会给你两个文本视图来处理。

回答by Android Android

Just remove this line:

只需删除此行:

 android:inputType="textMultiLine"

inputType for EditTexts

用于 EditTexts 的 inputType

回答by Chaoz

The TextView must have the 'singleLine' attribute set to false. Also you should set the 'ellipsize' to wrap the text:

TextView 必须将 'singleLine' 属性设置为 false。您还应该设置 'ellipsize' 来包装文本:

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

回答by Ezzored

You can do it like this:

你可以这样做:

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

回答by Karol ?yg?owicz

in layout

在布局中

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

回答by Manpreet Singh Dhillon

I did it following way:

我是这样做的:

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

回答by Gulzar Bhat

First replace "\n" with its Html equavalent "&lt;br&gt;" then call Html.fromHtml()on the string. Follow below steps:

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

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

This works perfectly.

这完美地工作。

回答by Simon Ninon

Adding to the answers: the order matters!

添加到答案:顺序很重要!

Make sure you call setInputType beforesetMinLines!

确保setMinLines之前调用 setInputType !