En Android cómo obtener el ancho de la vista de texto que se establece en Wrap_Content

Estoy intentando agregar un texto al textview para el cual he fijado el ancho como Wrap_content. Estoy tratando de obtener el ancho de este textview. Pero su 0 mostrando en todos los casos. Cómo puedo conseguir el ancho de la textview después de fijar el texto en él.

El código es como:

LinearLayout ll= new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); TextView tv = new TextView(this); tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); ll.addView(tv); tv.setText("Hello 1234567890-0987654321qwertyuio787888888888888888888888888888888888888888888888888"); System.out.println("The width is == "+tv.getWidth());// result is 0 this.setContentView(ll); 

Por favor recomiende. Gracias por adelantado.

Las vistas con el ancho / altura dinámico obtienen su tamaño correcto sólo después de que se haya completado el proceso de diseño ( http://developer.android.com/reference/android/view/View.html#Layout ).
Puede agregar OnLayoutChangeListener a su TextView y obtener su tamaño allí:

 tv.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { final int width = right - left; System.out.println("The width is == " + width); }); 

No se puede obtener el ancho de una Vista con tamaño dinámico antes de que el diseño esté completamente construido. Eso significa que no hay manera de que lo pueda obtener en onCreate (). Una forma sería crear una clase que hereda de TextView y onSizeChanged() .

¿Cuándo llamas a esto? ¿Ya se ha dibujado en la pantalla?

Parece que usted está llamando a getWidth() demasiado pronto.

También puede echar un vistazo a esta pregunta .

Esto funciona para mí:

 RelativeLayout.LayoutParams mTextViewLayoutParams = (RelativeLayout.LayoutParams) mTextView.getLayoutParams(); mTextView.setText(R.string.text); mTextView.measure(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); int width = mShareTip.getMeasuredWidth(); //use the width to do what you want mShareTip.setLayoutParams(mShareTipLayoutParams); 

Usted puede intentar esto:

 textView.measure(0,0); int width = textView.getMeasuredWidth(); 

Usted debe usar esto:

TextView.getMeasuredWidth ();

  • TextView sombra destruida mientras captura la imagen
  • Selección de texto en TextView e invocación de menú flotante en un clic largo
  • Cómo agregar una ruptura de línea en un Android TextView?
  • Establecer dinámicamente vínculos a texto en strings.xml
  • TextView en la Galería
  • Android ¿Cómo enviar texto e imágenes o cualquier objeto con intención?
  • Efecto de sombra para un texto en Android?
  • ¿Hay una manera de agregar la sombra interna a un TextView en Android?
  • Obtener posición absoluta para un desplazamiento dado en TextView (Android)
  • Zoom textview con imagen y texto (contenido html) en android
  • Desplazamiento horizontal automático en TextView
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.