TextView android: ellipsize = "end" issue

En mi aplicación, he ellipsize d mi TextView para que si el texto es demasiado grande, se mostrará ... al final utilizando android:ellipsize="end" .

Por ejemplo, el texto ABCDEFGHIJKLMNOPQRSTUVWXYZ se muestra como ABCDEFGHIJ... Pero cuando el texto es pequeño, como ABCDEF todavía muestra ... al final haciendo que el texto parezca AB... No puedo fijar el ancho de la vista de texto ya que la misma vista de texto se utiliza en otras partes con algunos requisitos de anchura diferentes. ¿Qué debo hacer para que funcione y muestre ... sólo si el texto es lo suficientemente grande.

// En su vista de texto

Agregue los siguientes atributos también

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

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

  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); } } }); 
  • ¿Cómo usar textview.getLayout ()? Devuelve null
  • Cómo quitar el guión del primer carácter en la vista de texto de Android
  • Android TextView setTextSize aumenta incorrectamente el tamaño del texto
  • android append '...' al final textview
  • Cómo hacer una vista de seguir a mi dedo con onScroll y GestureDetector - Android
  • Desplazamiento de un TextView a una línea específica
  • Uso de varios colores de texto en la vista de texto de Android
  • ¿Cómo alinear el texto con sus elementos compuestos en TextView?
  • Actualizar Textview desde la función Android
  • El texto (fuente) parece desvanecido en los apis más antiguos
  • Cómo hacer un TextView en negrita?
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.