2012-03-26 38 views
5

En mi aplicación, tengo ellipsize d mi TextView de modo que si el texto es demasiado grande, mostrará ... al final usando android:ellipsize="end".TextView android: ellipsize = "end" issue

por ejemplo, texto ABCDEFGHIJKLMNOPQRSTUVWXYZ se muestra como ABCDEFGHIJ.... Pero cuando el texto es pequeño, como ABCDEF, todavía muestra ... al final haciendo que el texto se vea como AB.... No puedo corregir el ancho de la vista de texto ya que la misma vista de texto se usa en otros lugares con algunos requisitos de ancho diferentes. ¿Qué debo hacer para que funcione y mostrar ... solo si el texto es lo suficientemente grande?

+0

muéstranos el XML que estás usando para la vista de texto. Tengo la sensación de que estás usando 'android: layout_width =" wrap_content "' – Blundell

+0

Puedes probar con android: ellipsize = "marquee". – YuviDroid

+0

con 'android: layout_width =" wrap_content "' todavía no debe elipsear el texto ... Tal vez cualquier contenedor primario tenga una limitación de ancho ... – pleerock

Respuesta

12

// En su TextView

añadir el siguiente atributos también

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

NOTA: se puede ajustar el tamaño de EMS a la cantidad de caracteres que desea mostrar.

+2

Muchas gracias. Eso hace el truco. – Rajkiran

+2

Respuesta útil. Sin embargo, el EMS no se convierte directamente en número de caracteres. –

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

esto reduce mi número de líneas a 2. también se puede configurar para 3 líneas. y ellipsize también funciona bien. –

Cuestiones relacionadas