6

I have a textview that I am programmatically setting maxLines with and then setting it to android:ellipsize="end". It works partially, but for some reason the text that is being truncated continues for a few characters after the ellipsis in 4.2.2. It is the actual text that is being truncated, not new characters.

It also is not working properly in 2.3.6 as it adds an ellipsis for each section with a line break or "\n".

I am not using any custom fonts, I have seen that has caused problems before.

Here is where I calculate and set the maxlines:

final TextView reviewView = (TextView) view.findViewById(R.id.textViewPosition3);

ViewTreeObserver observer = reviewView.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        int maxLines = (int) reviewView.getHeight() / reviewView.getLineHeight();
        reviewView.setMaxLines(maxLines);
        reviewView.setEllipsize(TruncateAt.END);
        reviewView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        //reviewView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
    }
});

Here is my xml:

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragmentTestLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <ImageView 
        android:id="@+id/restaurantImage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/test"
        android:layout_marginTop="-10dp"
        android:layout_marginRight="20dp"
        android:layout_marginLeft="20dp"
        />    

    <ImageView 
        android:id="@+id/squareBlack"
        android:layout_alignRight="@id/restaurantImage"
        android:layout_alignTop="@id/restaurantImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/square_black_small"
        />  

     <TextView
        android:id="@+id/textViewPosition"
        android:layout_alignRight="@id/squareBlack"
        android:layout_alignLeft="@id/squareBlack"
        android:layout_alignBottom="@id/squareBlack"
        android:layout_alignTop="@id/squareBlack"
        android:layout_margin="1dp"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is a test"
        android:textSize="25sp" 
        android:textColor="#a6cbff"
        android:textStyle="bold"
        />

     <ImageView 
        android:id="@+id/squareBlue"
        android:layout_toLeftOf="@id/squareBlack"
        android:layout_alignTop="@id/squareBlack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/square_blue_small"
        /> 

     <TextView
        android:id="@+id/textViewPosition1"
        android:layout_alignRight="@id/squareBlue"
        android:layout_alignLeft="@id/squareBlue"
        android:layout_alignBottom="@id/squareBlue"
        android:layout_alignTop="@id/squareBlue"
        android:layout_margin="1dp"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="7.5"
        android:textSize="25sp" 
        android:textColor="#902515"
        android:textStyle="bold"
        />

     <TextView
        android:id="@+id/restaurantName"
        android:layout_alignLeft="@id/restaurantImage"
        android:layout_alignBottom="@id/restaurantImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Restaurant Name"
        android:textSize="25sp" 
        android:textColor="#fff"
        android:textStyle="bold"
        android:background="@drawable/greybar"
        />

     <TextView
        android:id="@+id/textViewPosition2"
        android:layout_below="@id/restaurantImage"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="February 15, 2013"
        android:textColor="#555"
        android:textStyle="bold"
        android:textSize="15sp" 
        />

      <Button android:id="@+id/readReview"
        android:background="@drawable/readreview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Read the Review" 
        android:textColor="#fff"
        android:textColorHighlight="#ccc"
        android:textSize="15sp"
        android:textStyle="bold"
        android:layout_centerHorizontal="true"
        />

      <TextView
        android:id="@+id/textViewPosition3"
        android:layout_below="@id/textViewPosition2"
        android:layout_above="@id/readReview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:textColorLink="#C90404"
        android:layout_marginRight="15dp"
        android:layout_marginLeft="15dp"
        android:textColor="#000"
        />



</RelativeLayout>
4

2 回答 2

2

我以编程方式解决了这个问题,但我不确定它是否是最好的解决方案,因为它感觉更像是一种 hack。它似乎正在工作。

        ViewTreeObserver observer = reviewView.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            int maxLines = (int) reviewView.getHeight() / reviewView.getLineHeight();

            reviewView.getViewTreeObserver().removeGlobalOnLayoutListener(this);


            if(reviewView.getLineCount() > maxLines)
            {
                int lineEndIndex = reviewView.getLayout().getLineEnd(maxLines - 1);
                String text = reviewView.getText().subSequence(0, lineEndIndex-3) +" ...";
                reviewView.setText(text);
            }
        }
    });
于 2013-09-05T19:21:38.550 回答
0

把它放在xml中

    android:singleLine="true"
    android:ellipsize="end"             
    android:maxLines="1"
于 2013-05-15T07:49:23.163 回答