Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
851 views
in Technique[技术] by (71.8m points)

android - ellipsize on textview doesnt work

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:

enter image description here

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...