How to set maximum characters per line for text view in android

后端 未结 7 1965
半阙折子戏
半阙折子戏 2020-12-16 15:09

Hiii all, this may be a possible duplicate but I couldn\'t find solution for my problem.I need to limit the number of characters per line when displaying on the textview and

相关标签:
7条回答
  • 2020-12-16 15:51

    use android:maxWidth="10dip" to limit the size of the textview. With this limitation the next line (assume you used setSingleLine(false)) will start right after the 10dip.

    Using android:maxLength="50" for example would limit your textview, but doesn't ensure that it breaks after X characters

    0 讨论(0)
  • 2020-12-16 15:54

    Set these parameters of TextView:

    android:layout_width="110dip"
    android:singleline="false"
    

    Experiment with the value of android:layout_width to find the value that fits your needs.

    0 讨论(0)
  • 2020-12-16 15:54

    Use android:maxLength="" to limit the text length

    <TextView
            android:id="@+id/textViewName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="20dp"
            android:gravity="center"
            android:text="Name"
            android:maxLength="12"
            android:textColor="#000000"
            android:textStyle="bold" />
    

    You can also use another property as follows:

    android:ellipsize="end"
    android:maxLines="1"
    

    show result :

    "Hello How are ..." instead of "Hello How are you?"

    0 讨论(0)
  • 2020-12-16 15:57

    Using

    android:maxWidth="size"
    

    Replace the "size" with the size you prefer. For example, 100dip.

    0 讨论(0)
  • 2020-12-16 15:59

    use ConstraintLayout and android:layout_width="0dp". The text will adapt to the constraints

    0 讨论(0)
  • 2020-12-16 16:02

    Use this code:

    android:maxLines="5"
    android:maxLength="10"
    

    Five line with max length of 10 character.

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