2012-03-26 8 views
5

Trong ứng dụng của tôi, tôi đã ellipsize d TextView để nếu văn bản quá lớn, nó sẽ hiển thị ... ở cuối bằng cách sử dụng android:ellipsize="end".TextView android: ellipsize = "end" issue

ví dụ: văn bản ABCDEFGHIJKLMNOPQRSTUVWXYZ được hiển thị là ABCDEFGHIJ.... Nhưng khi văn bản nhỏ, như ABCDEF nó vẫn hiển thị ... ở cuối làm cho văn bản trông giống như AB.... Tôi không thể sửa chiều rộng của textview vì cùng một textview được sử dụng ở nơi khác với một số yêu cầu chiều rộng khác nhau. Tôi nên làm gì để làm cho nó hoạt động và hiển thị ... chỉ khi văn bản đủ lớn.

+0

hiển thị cho chúng tôi XML bạn đang sử dụng cho chế độ xem văn bản. Tôi có cảm giác bạn đang sử dụng 'android: layout_width =" wrap_content "' – Blundell

+0

Bạn có thể thử với android: ellipsize = "marquee". – YuviDroid

+0

với 'android: layout_width =" wrap_content "' nó vẫn không nên bỏ qua văn bản ... Có thể bất kỳ vùng chứa nào có giới hạn chiều rộng .. – pleerock

Trả lời

12

// Trong TextView bạn

thêm bên dưới thuộc tính cũng

android:maxEms="8" 
android:singleLine="true" 

LƯU Ý: bạn có thể điều chỉnh kích thước ems để có bao nhiêu ký tự bạn muốn hiển thị.

+2

Cảm ơn rất nhiều. Điều đó làm các trick. – Rajkiran

+2

Câu trả lời hữu ích. EMS mặc dù không trực tiếp chuyển đổi thành số ký tự. –

1
txtvw.setText("The Indian economy is the world's eleventh-largest by nominal GDP and third-largest by purchasing power parity (PPP). " + 
       "  Following market-based economic reforms in 1991, India became one of the fastest-growing major economies; " + 
       "  it is considered a newly industrialised country. However, it continues to face the challenges of poverty, illiteracy, corruption, malnutrition, and inadequate public healthcare. " + 
       "  A nuclear weapons state and a regional power, it has the third-largest standing army in the world and ranks ninth in military expenditure among nations." + 
       "  India is a federal constitutional republic governed under a parliamentary system consisting of 28 states and 7 union territories. " + 
       "  India is a pluralistic, multilingual, and multiethnic society. " + 
       "  It is also home to a diversity of wildlife in a variety of protected habitats."); 
     ViewTreeObserver vto = txtvw.getViewTreeObserver(); 
     vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 

      @Override 
      public void onGlobalLayout() { 
       ViewTreeObserver obs = txtvw.getViewTreeObserver(); 
       obs.removeGlobalOnLayoutListener(this); 
       if(txtvw.getLineCount() > 1){ 

        int lineEndIndex = txtvw.getLayout().getLineEnd(1); 
        String text = txtvw.getText().subSequence(0, lineEndIndex-3)+"..."; 
        txtvw.setText(text); 

       } 

      } 
     }); 
+0

điều này làm giảm số dòng của tôi xuống 2. nó có thể được thiết lập cho 3 dòng cũng. và hình elip cũng hoạt động tốt. –