How to wrap text in textview in Android

前端 未结 18 1053
旧巷少年郎
旧巷少年郎 2020-12-08 18:03

Does any one know how to wrap text in TextView in Android platform. i.e if the text in textview exceed the screen length it should be displayed in the second line.

I

相关标签:
18条回答
  • 2020-12-08 18:43

    Try @Guykun's approach

    android:layout_weight="1" android:ellipsize="none" android:maxLines="100" android:scrollHorizontally="false"

    Also, make sure that parents width is not set to wrap content. This is the thing that I was missing.

    0 讨论(0)
  • 2020-12-08 18:44

    Just was working on a TextView inside a layout inside a RecyclerView. I had text getting cut off, ex, for Read this message, I saw: Read this. I tried setting android:maxLines="2" on the TextView, but nothing changed. However, android:lines="2" resulted in Read this on first line and message on the 2nd.

    0 讨论(0)
  • 2020-12-08 18:44

    The trick is with the textView width, try to make it dedicated number like:

    <TextView
        android:layout_width="300dp"
        android:layout_height="wrap_content"/>
    

    I've tried many solutions without any result, I've tried:

        android:ellipsize="none"
        android:scrollHorizontally="false"
    

    the only one thing triggred the wrap option is the dedicated width

    0 讨论(0)
  • 2020-12-08 18:47

    I am using Android 2.2 and my textview will automatically goto the next line if it exceeds the screen.

    If you would like to have the text goto the next line before the end of the screen, just add in (just put in your own dp value). This will be useful if you have a picture on the right of the text.

    android:layout_marginRight="52dp"
    
    0 讨论(0)
  • 2020-12-08 18:49

    Constraint Layout

    <TextView
    android:id="@+id/some_textview"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    
    app:layout_constraintLeft_toLeftOf="@id/textview_above"
    app:layout_constraintRight_toLeftOf="@id/button_to_right"/>
    
    • Ensure your layout width is zero
    • left / right constraints are defined
    • layout height of wrap_content allows expansion up/down.
    • Set android:maxLines="2" to prevent vertical expansion (2 is just an e.g.)
    • Ellipses are prob. a good idea with max lines android:ellipsize="end"

    0dp width allows left/right constraints to determine how wide your widget is.

    Setting left/right constraints sets the actual width of your widget, within which your text will wrap.

    Constraint Layout docs

    0 讨论(0)
  • 2020-12-08 18:49

    By setting android:maxEms to a given value together with android:layout_weight="1" will cause the TextView to wrap once it reaches the given length of the ems.

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