Tamaño de texto de escala automática

Estoy buscando una manera de cuando cambio un tamaño de pantalla proporcionalmente redimensionar el texto. En la actualidad he intentado Auto Text Text escala para caber dentro de límites, pero no parece estar funcionando. Pero no estoy seguro de hacerlo de la manera correcta o no. Ahora estoy llamando por

AutoResizeTextView test = new AutoResizeTextView(this); test=(AutoResizeTextView)findViewById(R.id.test456); test.resizeText(); // I call this to resize.. am I right? 

XML

 <com.mypackage.AutoResizeTextView android:id="@+id/test456" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" /> 

No es necesario llamar a AutoResizeTextView test , puede decir TextView ya que la clase extiende TextView . No veo por qué tendrías que llamar a resizeText() .

De cualquier manera, aquí hay una clase personalizada que me gusta usar para volver a clasificar el texto automáticamente.

 public class AutoFitTextView extends TextView { public AutoFitTextView(Context context) { super(context); init(); } public AutoFitTextView(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { maxTextSize = this.getTextSize(); if (maxTextSize < 35) { maxTextSize = 30; } minTextSize = 20; } private void refitText(String text, int textWidth) { if (textWidth > 0) { int availableWidth = textWidth - this.getPaddingLeft() - this.getPaddingRight(); float trySize = maxTextSize; this.setTextSize(TypedValue.COMPLEX_UNIT_PX, trySize); while ((trySize > minTextSize) && (this.getPaint().measureText(text) > availableWidth)) { trySize -= 1; if (trySize <= minTextSize) { trySize = minTextSize; break; } this.setTextSize(TypedValue.COMPLEX_UNIT_PX, trySize); } this.setTextSize(TypedValue.COMPLEX_UNIT_PX, trySize); } } @Override protected void onTextChanged(final CharSequence text, final int start, final int before, final int after) { refitText(text.toString(), this.getWidth()); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { if (w != oldw) { refitText(this.getText().toString(), w); } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int parentWidth = MeasureSpec.getSize(widthMeasureSpec); refitText(this.getText().toString(), parentWidth); } public float getMinTextSize() { return minTextSize; } public void setMinTextSize(int minTextSize) { this.minTextSize = minTextSize; } public float getMaxTextSize() { return maxTextSize; } public void setMaxTextSize(int minTextSize) { this.maxTextSize = minTextSize; } private float minTextSize; private float maxTextSize; } 

Yo uso esto, proporcionalmente en el ancho de la pantalla.

 DisplayMetrics displaymetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); width_screen=displaymetrics.widthPixels; Mytext.setTextSize(TypedValue.COMPLEX_UNIT_PX, (width_screen/CONST)); 

El CONST es un número que utilizo para escalar la fuente, en la dimensión que quiero. Funciona para mis necesidades.

¡Buenas noticias! Google presentó el soporte Autosizing TextView en Android O. También se incluirá en bibliotecas de soporte.

https://developer.android.com/preview/features/autosizing-textview.html

  • Actualizar Android TextView
  • Android: ClickableSpan en TextView seleccionable
  • TextView.setTextSize se comporta anormalmente - Cómo establecer el tamaño de texto de textview dinámicamente para diferentes pantallas
  • Escala textSize en un TextView
  • Ellipsize no funciona para textView dentro de listView personalizado
  • Android: Colorear parte de una cadena usando TextView.setText ()?
  • La vista de texto cambia el color de fondo sin ninguna razón
  • Evitar que se rompa la palabra en TextView
  • Android dibujar círculo alrededor de texto
  • Establecer ancho a wrap_content para TextView a través de código
  • Pellizcar Zoom en el texto de vista Android
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.