【问题标题】:How to make a TextView multiline programmatically in Android (Java)如何在 Android (Java) 中以编程方式制作 TextView 多行
【发布时间】:2015-06-09 13:47:31
【问题描述】:

我想让我的 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>

【问题讨论】:

  • 在发布此类问题之前请先做一些研究......
  • @MurtazaKhursheedHussain 我三天后就遇到了这个问题:stackoverflow.com/questions/15032870/… 我检查了类似的东西,我认为有 10-20 个站点,但它不起作用

标签: java android textview multiline


【解决方案1】:

我是按照以下方式完成的:

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

【讨论】:

    【解决方案2】:

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

    <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
    

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

    【讨论】:

    【解决方案3】:

    你可以这样做:

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

    【讨论】:

      【解决方案4】:

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

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

      这很好用。

      【讨论】:

        【解决方案5】:

        补充答案:顺序很重要!

        确保在 setMinLines 之前调用 setInputType!

        【讨论】:

          【解决方案6】:

          TextView 必须将“singleLine”属性设置为 false。你也应该设置'ellipsize'来换行:

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

          【讨论】:

          • 现在,最后有 3 个点,但它也在一行:(
          【解决方案7】:

          在布局中

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

          【讨论】:

            【解决方案8】:

            只需删除这一行:

             android:inputType="textMultiLine"
            

            EditTexts 的输入类型

            【讨论】:

              猜你喜欢
              • 2016-04-09
              • 2011-01-28
              • 2017-07-09
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多