Android, How to limit width of TextView (and add three dots at the end of text)?

前端 未结 21 1828
野的像风
野的像风 2020-12-02 03:44

I have a TextView that I want to limit characters of it. Actually, I can do this but the thing that I\'m looking for is how to add three dots (...) at the end o

相关标签:
21条回答
  • 2020-12-02 04:18

    The following is what I learned by playing around with various options for forcing a TextView to a single line (with and without the three dots).

    enter image description here

    android:maxLines="1"

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:text="one two three four five six seven eight nine ten" />
    

    This just forces the text to one line. Any extra text is hidden.

    Related:

    • android:maxLines
    • android:singleLine (Note this and this)
    • android:lines

    ellipsize="end"

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:ellipsize="end"
        android:text="one two three four five six seven eight nine ten" />
    

    This cuts off the text that doesn't fit but lets users know that the text has been truncated by adding an ellipsis (the three dots).

    Related:

    • ellipsize="start" (...aaabbbccc)
    • ellipsize="middle" (aaa...ccc)
    • android: Ellipsise , meaning of the options

    ellipsize="marquee"

    <TextView
        android:id="@+id/MarqueeText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:text="one two three four five six seven eight nine ten" />
    

    This makes the text scroll automatically across the TextView. Note that sometimes it needs to be set in code:

    textView.setSelected(true);
    

    Supposedly android:maxLines="1" and android:singleLine="true" should do basically the same thing and since singleLine is apparently deprecated I would prefer not to use it, but when I take it out, the marquee doesn't scroll anymore. Taking maxLines out doesn't affect it, though.

    Related:

    • Marquee text in Android

    HorizontalScrollView with scrollHorizontally

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/horizontalScrollView">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:scrollHorizontally="true"
            android:text="one two three four five six seven eight nine ten" />
    </HorizontalScrollView>
    

    This allows the user to manually scroll to see the whole line of text.

    0 讨论(0)
  • 2020-12-02 04:19

    Use

    • android:singleLine="true"
    • android:maxLines="1"
    • app:layout_constrainedWidth="true"

    It's how my full TextView looks:

        <TextView
        android:id="@+id/message_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="5dp"
        android:maxLines="1"
        android:singleLine="true"
        android:text="NAME PLACEHOLDER MORE Text"
        android:textColor="@android:color/black"
        android:textSize="16sp"
    
        android:textStyle="bold"
        app:layout_constrainedWidth="true"
        app:layout_constraintEnd_toStartOf="@id/message_check_sign"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintStart_toEndOf="@id/img_chat_contact"
        app:layout_constraintTop_toTopOf="@id/img_chat_contact" />
    

    Picture of <code>TextView</code>

    0 讨论(0)
  • 2020-12-02 04:21

    Deprecated:

    Add one more property android:singleLine="true" in your Textview

    Updated:

    android:ellipsize="end" 
    android:maxLines="1"
    
    0 讨论(0)
  • 2020-12-02 04:21

    You need to add following lines into your layout for the textview

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

    Hope this works for you.

    0 讨论(0)
  • 2020-12-02 04:22

    code:

    TextView your_text_view = (TextView) findViewById(R.id.your_id_textview);
    your_text_view.setEllipsize(TextUtils.TruncateAt.END);
    

    xml:

    android:maxLines = "5"
    

    e.g.

    In Matthew 13, the disciples asked Jesus why He spoke to the crowds in parables. He answered, "It has been given to you to know the mysteries of the kingdom of heaven, but to them it has not been given.

    Output: In Matthew 13, the disciples asked Jesus why He spoke to the crowds in parables. He answered, "It has been given to you to know...

    0 讨论(0)
  • 2020-12-02 04:25

    Try this property of TextView in your layout file..

    android:ellipsize="end"
    android:maxLines="1"
    
    0 讨论(0)
提交回复
热议问题