End TextView with 3 dots with use of maxLength

前端 未结 11 812
北海茫月
北海茫月 2020-12-13 06:08

I have a TextView in my layout which is wrap_content in layout_width. It is limited to maximum of 15 characters so I\'m using maxLength.

I need to end this TextView

相关标签:
11条回答
  • 2020-12-13 06:26

    I gather (from comment) that @Yaniv already solved it using code - but this is the right way to do it (with xml). May help other users who land here. Trick is to use both toleftof and torightof.

    <RelativeLayout>
    ...
     <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:singleLine="true"
            android:layout_toRightOf="@id/some_element1"
            android:layout_toLeftOf="@id/some_element2"/>
    ...
    <RelativeLayout>
    
    0 讨论(0)
  • 2020-12-13 06:27

    This will solve your problem, Use ellipsize Property in your XML Code

    android:ellipsize="end" <!-- This makes the magic ... thing -->
    android:maxEms="15" <!-- Limit of the Text -->
    android:singleLine="true" <!-- In case if you want everything in one line -->
    

    Edit: singleLine is deprecated. Use maxlines="1" instead.

    0 讨论(0)
  • 2020-12-13 06:30

    I needed to do this with Radio Button and I wanted to limit the size to one line. When I used Android:singleline="true", radio button's check circle disappeared. Here's the code that finally worked:

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

    There's no need to set ems. This will probably work also in TextView and other UI components. Hope this helps to someone.

    0 讨论(0)
  • 2020-12-13 06:31

    Tried most of the solutions above. Using maxWidth was the key for me:

    android:maxWidth="100dp"
    android:maxLines="1"
    android:ellipsize="end"
    
    0 讨论(0)
  • 2020-12-13 06:34

    One of the easiest way is to add Right Padding + Ellipsize

    <TextView
                android:id="@+id/txtvw_contentcell_subhead"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Polem sampler, Lorem Ipsum golep tolem burop yemit noski"
                android:ellipsize="end"
                android:maxLines="1"
                android:textColor="@color/caption_black_color"
                android:textSize="@dimen/caption_size"
                android:paddingRight="40dp"/>
    

    Here is an example Subtitle Text with char limit.

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