2012-03-26 9 views
5

Nella mia applicazione, ho ellipsize d mio TextView in modo che se il testo è troppo grande, che verrà visualizzato ... alla fine utilizzando android:ellipsize="end".TextView Android: ellipsize = "end" questione

ad esempio il testo ABCDEFGHIJKLMNOPQRSTUVWXYZ viene visualizzato come ABCDEFGHIJ.... Ma quando il testo è piccolo, come ABCDEF mostra ancora ... alla fine rendendo il testo simile a AB.... Non riesco a correggere la larghezza della vista testo poiché la stessa textview è usata altrove con alcuni requisiti di larghezza diversi. Cosa devo fare per farlo funzionare e mostrare ... solo se il testo è abbastanza grande.

+0

mostraci l'XML che usi per la visualizzazione di testo. Ho la sensazione che stai usando 'android: layout_width =" wrap_content "' – Blundell

+0

Puoi provare con Android: ellipsize = "tendone". – YuviDroid

+0

con 'android: layout_width =" wrap_content "' ancora non dovrebbe ellissi testo ... Forse qualsiasi contenitore genitore ha una larghezza limitazioni .. – pleerock

risposta

12

// Nella tua TextView

aggiungere il seguito attributi anche

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

NOTA: è possibile regolare le dimensioni di SME al numero di caratteri che si desidera visualizzare.

+2

Grazie mille. Questo fa il trucco. – Rajkiran

+2

Risposta utile. Tuttavia, EMS non converte direttamente in numero di caratteri. –

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

questo riduce il mio numero di linee a 2. può essere impostato anche per 3 linee. e ellissi funziona anche bene. –