studio que programacion programa móviles hacer desarrollo curso como aplicaciones android textview

android - que - Obtener la altura de la vista de texto antes de renderizar en el diseño



render programa (5)

No se pudo encontrar una buena solución para calcular la altura de la vista de texto donde se estableció el texto antes de mostrar la vista de texto al diseño. Cualquier ayuda por favor


2 soluciones

Usó la solución 1 al principio y encontró la solución 2 más adelante. Ambos funcionan, es realmente lo que prefieres.

Importante es asegurarse de tener todas las dimensiones correctas ya que mezclar tamaños de fuente en sp o px hará una gran diferencia dependiendo de la pantalla en la que realice la prueba.

Un proyecto de ejemplo muy básico está disponible en https://github.com/hanscappelle/SO-3654321

Solución 1 usando TextView y MeasureSpec

El problema principal con la pregunta original es que TextView en el siguiente método debe configurarse como nuestro TextView, que debe representarse en el diseño. Creo que esta solución es valiosa para muchas personas que enfrentan este problema.

public static int getHeight(Context context, CharSequence text, int textSize, int deviceWidth, Typeface typeface,int padding) { TextView textView = new TextView(context); textView.setPadding(padding,0,padding,padding); textView.setTypeface(typeface); textView.setText(text, TextView.BufferType.SPANNABLE); textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize); int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(deviceWidth, View.MeasureSpec.AT_MOST); int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); textView.measure(widthMeasureSpec, heightMeasureSpec); return textView.getMeasuredHeight(); }

Y un ejemplo de cómo usar esto:

// retrieve deviceWidth int deviceWidth; WindowManager wm = (WindowManager) textView.getContext().getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2){ Point size = new Point(); display.getSize(size); deviceWidth = size.x; } else { deviceWidth = display.getWidth(); } // the text to check for String exampleTextToMeasure = "some example text that will be long enough to make this example split over multiple lines so we can''t easily predict the final height"; // some dimensions from dimes resources to take into account int textSize = getContext().getResources().getDimensionPixelSize(R.dimen.text_size); int padding = getContext().getResources().getDimensionPixelSize(R.dimen.text_padding); // final calculation of textView height int measuredTextHeight = getHeight(getContext(), exampleTextToMeasure, textSize, deviceWidth, TypeFace.DEFAULT, padding);

Solución 2 usando TextPaint y StaticLayout

Este método se basa en un TextPaint y StaticLayout que también proporciona resultados confiables en todos los niveles de API que he probado hasta ahora. Preste buena atención a las unidades de dimensiones; ¡Todo debería estar en píxeles!

Fuente: medición de la altura del texto que se dibujará en Canvas (Android)

public static int method1UsingTextPaintAndStaticLayout( final CharSequence text, final int textSize, // in pixels final int deviceWidth, // in pixels final int padding // in pixels ) { TextPaint myTextPaint = new TextPaint(); myTextPaint.setAntiAlias(true); // this is how you would convert sp to pixels based on screen density //myTextPaint.setTextSize(16 * context.getResources().getDisplayMetrics().density); myTextPaint.setTextSize(textSize); Layout.Alignment alignment = Layout.Alignment.ALIGN_NORMAL; float spacingMultiplier = 1; float spacingAddition = padding; // optionally apply padding here boolean includePadding = padding != 0; StaticLayout myStaticLayout = new StaticLayout(text, myTextPaint, deviceWidth, alignment, spacingMultiplier, spacingAddition, includePadding); return myStaticLayout.getHeight(); }


Aquí está mi solución fácil es obtener el tamaño antes de ser pintado

https://.com/a/40133275/1240672


Buena respuesta de @support_ms, pero no estoy seguro del punto de crear un nuevo TextView y de elaborar todos estos parámetros de entrada cuando podrías simplemente formatear tu TextView primero y luego llamar al método estático con solo un parámetro, el TextView mismo !

Además, no estoy seguro de por qué un parámetro fue etiquetado como deviceWidth , solo uso el ancho de Textview . El mío fue match_parent y supongo que cualquier TextView con wrap_content puede no funcionar en absoluto. Pero eso es lo que obtienes.

public static int getHeight(TextView t) { int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(screenWidth(t.getContext()), View.MeasureSpec.AT_MOST); int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); t.measure(widthMeasureSpec, heightMeasureSpec); return t.getMeasuredHeight(); } public static int screenWidth(Context context) { WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); return display.getWidth(); }


Desde la respuesta de support_ms, hay un método más simple que toma solo un TextView como parámetro.

/** * Get the TextView height before the TextView will render * @param textView the TextView to measure * @return the height of the textView */ public static int getTextViewHeight(TextView textView) { WindowManager wm = (WindowManager) textView.getContext().getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); int deviceWidth; if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2){ Point size = new Point(); display.getSize(size); deviceWidth = size.x; } else { deviceWidth = display.getWidth(); } int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(deviceWidth, View.MeasureSpec.AT_MOST); int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); textView.measure(widthMeasureSpec, heightMeasureSpec); return textView.getMeasuredHeight(); }


Obtener la línea de TextView antes de renderizar

Este es mi código base en la idea anterior. Está funcionando para mí.

private int widthMeasureSpec; private int heightMeasureSpec; private int heightOfEachLine; private int paddingFirstLine; private void calculateHeightOfEachLine() { WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); Point size = new Point(); display.getSize(size); int deviceWidth = size.x; widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(deviceWidth, View.MeasureSpec.AT_MOST); heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); //1 line = 76; 2 lines = 76 + 66; 3 lines = 76 + 66 + 66 //=> height of first line = 76 pixel; height of second line = third line =... n line = 66 pixel int heightOfFirstLine = getHeightOfTextView("A"); int heightOfSecondLine = getHeightOfTextView("A/nA") - heightOfFirstLine; paddingFirstLine = heightOfFirstLine - heightOfSecondLine; heightOfEachLine = heightOfSecondLine; } private int getHeightOfTextView(String text) { // Getting height of text view before rendering to layout TextView textView = new TextView(context); textView.setPadding(10, 0, 10, 0); //textView.setTypeface(typeface); textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources().getDimension(R.dimen.tv_size_14sp)); textView.setText(text, TextView.BufferType.SPANNABLE); textView.measure(widthMeasureSpec, heightMeasureSpec); return textView.getMeasuredHeight(); } private int getLineCountOfTextViewBeforeRendering(String text) { return (getHeightOfTextView(text) - paddingFirstLine) / heightOfEachLine; }

Nota: Este código también debe configurarse para una vista textual real en la pantalla

textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources().getDimension(R.dimen.tv_size_14sp));