[英]How to make a TextView multiline programmatically in Android (Java)
我想讓我的 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>
我是這樣做的:
tv.setElegantTextHeight(true);
tv.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
tv.setSingleLine(false);
您想在同一個文本視圖中顯示不同的文本嗎? 如果是這樣,請使用兩個文本視圖,例如:
<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
如果您要使用的此 textview 屬於 ListView,只需使用:
android.R.layout.simple_list_item_2
它會給你兩個文本視圖來處理。
你可以這樣做:
txt_tweet.setSingleLine(false);
txt_tweet.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION);
首先用它的 Html 等價物“ <br>
”替換“ \\n
”,然后在字符串上調用Html.fromHtml()
。 請按照以下步驟操作:
String text= model.getMessageBody().toString().replace("\n", "<br>")
textView.setText(Html.fromHtml(Html.fromHtml(text).toString()))
這完美地工作。
添加到答案:順序很重要!
確保在setMinLines之前調用 setInputType!
TextView 必須將 'singleLine' 屬性設置為 false。 您還應該設置 'ellipsize' 來包裝文本:
android:singleLine="false"
android:ellipsize="end"
在布局中
android:lines="8" //Total Lines prior display
android:minLines="6" //Minimum lines
android:maxLines="10" //Maximum Lines
只需刪除此行:
android:inputType="textMultiLine"
用於 EditTexts 的 inputType
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.