Multiline TextView in Android?

前端 未结 20 1333
南方客
南方客 2020-11-29 17:26

I did like below in xml


    

        
相关标签:
20条回答
  • 2020-11-29 18:13

    I like neither of the answers. Simply set the inputType and the TextView will adapt to its content

    <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="textMultiLine"/>
    

    Tested on a Nexus One (2.3) and Nexus 4 (4.4)

    0 讨论(0)
  • 2020-11-29 18:14

    I just thought I'd add that if you use :

    android:inputType="textMultiLine"

    Then the view stops being clickable. I was trying to get multi line textviews in my slide-drawer menu which obviously needs to respond to clicks.

    The android:singleLine="false" worked fine though.

    0 讨论(0)
  • 2020-11-29 18:14

    I do not like the solution that forces the number of lines in the text view. I rather suggest you solve it via the solution proposed here. As I see the OP is also struggling with making text view look like proper in table and shrinkColumns is the correct directive to pass in to achieve what is wanted.

    0 讨论(0)
  • 2020-11-29 18:16

    you can use android:inputType="textMultiLine"

      <TextView android:id="@+id/address1"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:gravity="left"
        android:maxLines="4" 
        android:inputType="textMultiLine"
        android:text="Johar Mor, Gulistan-e-Johar, Karachi" />
    
    0 讨论(0)
  • 2020-11-29 18:16

    for me non of the solutions did not work. I know it is not a complete answer for this question, but sometimes it can help.

    I put 4 textviews in a vertical linearlayout.

    <Linearlayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation:"vertical"
    
        ...>
        <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text:"line1"
           .../>
        <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text:"line2"
           .../>
        <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text:"line3"
           .../>
        <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text:"line4"
           .../>
    </LinearLayout>
    

    This solution is good for cases that the text view text is constant, like labels.

    0 讨论(0)
  • 2020-11-29 18:18

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

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

    This works perfectly.

    0 讨论(0)
提交回复
热议问题