ellipsize on textview doesnt work

前提是你 提交于 2019-12-12 00:55:42

问题


I want to display ellipsis if my textview length is too long. But for some reason, it doesnt seem to work. Please take a look at the image on how it looks like:

Here is my layout code.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView 
          android:id="@+id/txt1_trans"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textIsSelectable="true"
          android:bufferType="spannable"
          android:layout_marginLeft="10dp"
          android:singleLine="true"
          android:ellipsize="end"
          android:freezesText="true"
          android:focusable="true" 
          android:focusableInTouchMode="true"
          android:text="My Personal > Bank of America savings"
          android:textStyle="bold"
          android:textSize="18sp"
          android:layout_marginTop="5dp"
          android:textColor="#000000"/>

<TextView 
    android:layout_width="wrap_content" 
    android:id="@+id/TextView2"
    android:layout_height="wrap_content" 
    android:textSize="18sp"
    android:text="100$"
    android:textStyle="bold"
    android:layout_marginRight="20dp"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@id/txt1_trans"
    android:layout_alignBottom="@id/txt1_trans"/>

</RelativeLayout>

I want the textView on the left (txt1_trans) to display ellipsis if it takes up the space that textview on the right ("TextView2") occupies. Please let me know what I am doing wrong.

Thanks


回答1:


You can't use ellipsize with wrap_content as the width. You need to use either wrap_parent or preferably match_parent depending on what version of Android you're targeting.




回答2:


I found the problem with the layout. For the 2nd TextView on my layout, I have set

alignParentRight = true

And the 1st TextView has

width = "wrap_content"

Because of this, both these TextViews end up taking up common space. In order to avoid that, I set the first TextView to_LeftOf 2nd TextView . This ensures that they dont try to write on the same common space . Here is my correct layout :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="fill_parent"
  android:layout_height="fill_parent">
<TextView 
      android:id="@+id/txt1_trans"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textIsSelectable="true"
      android:bufferType="spannable"
      android:layout_marginLeft="10dp"
      android:singleLine="true"
      android:ellipsize="end"
      android:layout_toLeftOf="@+id/TextView2"
      android:freezesText="true"
      android:focusable="true" 
      android:focusableInTouchMode="true"
      android:text="My Personal Bank of America savings"
      android:textStyle="bold"
      android:textSize="18sp"
      android:layout_marginTop="5dp"
      android:textColor="#000000"/>

   <TextView 
      android:layout_width="wrap_content" 
      android:id="@+id/TextView2"
      android:layout_height="wrap_content" 
      android:textSize="18sp"
      android:text="100$"
      android:textStyle="bold"
      android:layout_marginRight="20dp"
      android:layout_alignParentRight="true"
      android:layout_alignTop="@id/txt1_trans"
      android:layout_alignBottom="@id/txt1_trans"/>
</RelativeLayout>


来源:https://stackoverflow.com/questions/21790188/ellipsize-on-textview-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!