2012-06-26 28 views
40

khi tôi đang sử dụng TextView với singleLine="true"ellipsize="end" (TextView đầu của tôi), nó hoạt động tốtThiết Ellipsize trên TextView giảm dòng thể hiện bởi một (thay vì chỉ ellipsizing cuối cùng)

nhưng trong khác TextView có nhiều hơn 1 dòng (trong trường hợp của tôi là 3 dòng ở dưới cùng của tôi TextView), lines="3" and maxLines="3" and ellipsize="end",không hoạt động đúng cách.

Khi tôi KHÔNG đặt ellipsize="end" trong tvDesc, nó hiển thị 3 dòng, là OK. Đây là mã: XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ImageView 
     android:id="@+id/imgv" 
     android:layout_width="65dp" 
     android:layout_height="80dp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:src="@drawable/img1" 
     android:scaleType="fitXY" /> 

    <TextView 
     android:id="@+id/tvTitle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_toRightOf="@+id/imgv" 
     android:background="@android:color/white" 
     android:textColor="@android:color/black" 
     android:text="Title testing line number and ellipsize at end" 
     android:maxLines="1" 
     android:singleLine="true" 
     android:ellipsize="end" <--- WORKS WELL 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <TextView 
     android:id="@+id/tvDesc" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/tvTitle" 
     android:layout_toRightOf="@+id/imgv" 
     android:layout_alignBottom="@+id/imgv" 
     android:layout_alignParentRight="true" 
     android:textSize="14dp" 
     android:lines="3" 
     android:maxLines="3" 
         <---------- WITHOUT ellipsize 

     android:text="I wanna this textview of having 3 lines and ellipsize at END and at this time I am Testing for it. This TextView shows 3 lines WITHOUT ellipsize property, but shows only 2 Lines when ELLIPSIZE property is setted" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

</RelativeLayout> 

Trên mã cho thấy TextView tvDesc với 3 dòng và Không-Ellipsize. Dưới đây là hình ảnh:

enter image description here

Nhưng, tôi muốn ellipsize, vì vậy tôi sử dụng mã followin: XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ImageView 
     android:id="@+id/imgv" 
     android:layout_width="65dp" 
     android:layout_height="80dp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:src="@drawable/img1" 
     android:scaleType="fitXY" /> 

    <TextView 
     android:id="@+id/tvTitle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_toRightOf="@+id/imgv" 
     android:background="@android:color/white" 
     android:textColor="@android:color/black" 
     android:text="Title testing line number and ellipsize at end" 
     android:maxLines="1" 
     android:singleLine="true" 
     android:ellipsize="end" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <TextView 
     android:id="@+id/tvDesc" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/tvTitle" 
     android:layout_toRightOf="@+id/imgv" 
     android:layout_alignBottom="@+id/imgv" 
     android:layout_alignParentRight="true" 
     android:textSize="14dp" 
     android:lines="3" 
     android:maxLines="3" 
     android:ellipsize="end" <------ WITH ELLIPSIZE 

     android:text="I wanna this textview of having 3 lines and ellipsize at END and at this time I am Testing for it. This TextView shows 3 lines WITHOUT ellipsize property, but shows only 2 Lines when ELLIPSIZE property is setted" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

</RelativeLayout> 

nào cho thấy TextView tvDesc chỉ với 2 dây chuyền và Ellipsize,

không phải là giao diện người dùng bị hủy bỏ đúng cách, như sau: enter image description here

Tôi muốn 3 dòng trong TextView tvDesc với ellipsize

BẤT CỨ AI CÓ THỂ GIÚP ME.?

+1

Tôi nghĩ rằng bạn đang chạy vào một [vấn đề đã biết] (http: // stackoverflow. com/questions/2160619/android-ellipsize-multiline-textview) – tiguchi

+0

Tôi không biết chính xác lý do tại sao điều này xảy ra. Nhưng nếu bạn muốn đi với ellipsize sau đó bạn giữ tài sản của nó không thay vì kết thúc. –

Trả lời

44

Đây là giải pháp đơn giản nhất mà tôi đã tìm thấy và hiện đang sử dụng trong triển khai. Hãy cho tôi biết nếu bạn cần bất kỳ sự trợ giúp nào khác!

Ồ và nhớ xóa thẻ android:ellipsize trong XML của bạn vì bạn sẽ sử dụng mã dưới cùng để tự động bỏ qua ở cuối 3 dòng.

TextView snippet; 
snippet.setText("loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor loren ipsum dolor ") 
ViewTreeObserver vto = this.snippet.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 

    @Override 
    public void onGlobalLayout() { 
     ViewTreeObserver obs = snippet.getViewTreeObserver(); 
     obs.removeGlobalOnLayoutListener(this); 
     if (snippet.getLineCount() > 3) { 
      int lineEndIndex = snippet.getLayout().getLineEnd(2); 
      String text = snippet.getText().subSequence(0, lineEndIndex - 3) + "..."; 
      snippet.setText(text); 
     } 
    } 
}); 
+0

Cảm ơn yaar ... Nó hoạt động. Cảm ơn rất nhiều. –

+0

Đây là một giải pháp tốt, nhưng có một cách dễ dàng hơn và sạch hơn, xem câu trả lời của tôi. – Flawyte

+1

Cảm ơn điều này đã làm việc cho tôi nhưng nó sẽ dễ dàng hơn nhiều nếu maxLines và ellipsize làm việc cùng nhau. – draksia

-2

Thử

tv.setSingleLine(false); 
tv.setEllipsize(TextUtils.TruncateAt.END); 
int n = 3; // the exact number of lines you want to display 
tv.setLines(n); 

tham khảo Programmatically create TextView with ellipsis

+0

Bạn đã kiểm tra điều này, nó không hoạt động cho tôi. – vrs

+0

Xem câu trả lời của Vishwa Patel, nó hoàn toàn hoạt động. –

+0

Xem chỉnh sửa. Tôi đã bỏ lỡ thêm hai dòng. tv.setSingleLine (sai); và tv.setLines (n); –

-1

Chỉ cần sử dụng ellipsize kết hợp với scrollHorizontally="true". Đơn giản & sạch sẽ.

Nó hoạt động hoàn hảo cho tôi.

+1

Điều này chỉ làm cho nó một dòng mà bạn đã có thể làm với tài sản 'singleLine' –

+0

@AliGangji Tôi không nghĩ rằng đó là trường hợp khi tôi đăng. Điều cần biết – Flawyte

+0

Đối với tôi giải pháp này hoạt động (Android 5.0) ngay cả đối với TextView đa dòng. Nhưng số dòng thay đổi giữa TextViews (từ 1 đến maxLines). – CoolMind

16

Chỉ cần đặt android:maxLinesandroid:ellipsize.

<TextView 
     android:id="@+id/tv_dua" 
     android:ellipsize="end" 
     android:maxLines="3" 
     android:text="long text" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 
+0

Trừ khi nó không hoạt động trên các phiên bản của Honeycomb. Những gì tôi nhận được là textview có kích thước đến 2 dòng, nhưng bạn nhận được các đỉnh của các chữ cái trên dòng thứ ba và không có dấu ba chấm. –

+0

Một nơi nào đó xung quanh KitKat hoặc Lollipop này bắt đầu hoạt động chính xác (tôi quên mức API chính xác). Trước đó nó vẫn giới hạn chính xác 'maxLines' nhưng không bao gồm dấu chấm lửng quá. Tuy nhiên, tôi nghĩ rằng đó là phần nào hợp lý những ngày này để sống với điều này vì nó là một lựa chọn đơn giản hơn nhiều để thao tác chuỗi thủ công trong mã. –

2

Sử dụng sau đây để có được một TextView multiline với lược trên dòng cuối cùng:

android:maxLines="4" 
android:ellipsize="end" 
android:singleLine="false" 

Thay 4 với số dòng bạn muốn. Hy vọng điều đó sẽ hữu ích!

0

tôi đã cố gắng nó ra với Maxlines và ellipsize trên Android 7 & 8.

android:maxLines="3" 
android:ellipsize="end" 

Các xem trước hiển thị 2 dòng và trên dòng thứ ba "...".

Nhưng điều này có vẻ là một lỗi trong trình xem trước.

Trên thiết bị nó chỉ hoạt động tốt, 3 dòng với văn bản, ở phần cuối của dòng thứ ba "..."

Các vấn đề liên quan