2012-03-26 44 views
5

在我的應用程序中,我有ellipsize d我的TextView,所以如果文字太大,最後會顯示...使用android:ellipsize="end"TextView android:ellipsize =「end」issue

例如文本ABCDEFGHIJKLMNOPQRSTUVWXYZ顯示爲ABCDEFGHIJ...。但是當文字很小時,如ABCDEF它最後仍會顯示...使文字看起來像AB...。我無法修復textview的寬度,因爲相同的textview在其他地方使用時有一些不同的寬度要求。我應該做些什麼才能使它工作,並且只有當文字足夠大時才顯示...

+0

向我們展示了您用於textview的XML。我有一種感覺,你使用'android:layout_width =「wrap_content」' – Blundell 2012-03-26 11:06:24

+0

你可以嘗試使用android:ellipsize =「marquee」。 – YuviDroid 2012-03-26 11:07:29

+0

with'android:layout_width =「wrap_content」'它仍然不應該ellipsize文本...也許任何父容器有一個寬度的限制.. – pleerock 2012-03-26 11:11:03

回答

12

//在你的TextView

還添加以下屬性

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

注:可以調整EMS大小,你要多少個字符顯示。

+2

非常感謝。這就是訣竅。 – Rajkiran 2012-03-26 12:05:15

+2

有用的答案。 EMS不直接轉換爲字符數。 – 2014-05-01 16:52:07

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

這會將我的行數減少到2.它也可以設置爲3行。和ellipsize也工作正常。 – 2012-09-04 13:47:56