texto tamaño studio programacion móviles libro letra desarrollo curso centrar cambiar aplicaciones android textview android-custom-view

tamaño - El texto personalizado de la vista de texto de android no está centrado



manual de programacion android pdf (1)

Intento crear una vista de texto que siempre será cuadrada, así que he implementado esta clase personalizada.

public class SquareTextView extends TextView { public SquareTextView(Context context) { super(context); } public SquareTextView(Context context, AttributeSet attrs) { super(context, attrs); } public SquareTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int max = Math.max(getMeasuredWidth(), getMeasuredHeight()); setMeasuredDimension(max, max); } }

Aquí hay un diseño de ejemplo que ilustra el problema:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="8dp" android:paddingLeft="16dp" android:paddingRight="16dp" android:paddingTop="8dp"> <com.mypackage.SquareTextView android:layout_width="wrap_content" android:layout_height="50dp" android:layout_gravity="right|top" android:background="#000" android:gravity="center" android:padding="4dp" android:text="1" android:textColor="#FFF"/> </LinearLayout>

Aquí hay una imagen de esto

Esto funciona muy bien para obtener la vista al cuadrado, sin embargo, parece que la gravedad se ensucia. Con esto, el texto parece estar siempre en la esquina superior izquierda. ¿Cómo puedo tener una vista de texto que siempre será cuadrada pero que mantenga la gravedad o al menos poder centrar el texto?

EDITAR: Después de algunas pruebas, he notado que si configuras el ancho o la altura a un tamaño de dp específico, la gravedad parece estar funcionando nuevamente. Entonces probablemente tiene que ver con el atributo WRAP_CONTENT . ¿Se manejará de otra manera en el método onmeasure que podría causar que mi propio método no funcione como se esperaba?


Espero que ya tengas la respuesta por ahora. Si no, puedes usar esto:

public class TextAlphaSquareTextView extends AppCompatTextView { private int mTextAlpha = 0; private boolean isSquare = false; public TextAlphaSquareTextView(Context context) { super(context); init(null); } public TextAlphaSquareTextView(Context context, AttributeSet attrs) { super(context, attrs); init(attrs); } public TextAlphaSquareTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(attrs); } private void init(AttributeSet attrs) { if (attrs == null) { } else { TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.TextAlphaSquareTextView); mTextAlpha = a.getInteger(R.styleable.TextAlphaSquareTextView_textAlpha, 100); isSquare = a.getBoolean(R.styleable.TextAlphaSquareTextView_squareMode, false); a.recycle(); if(mTextAlpha < 0 || mTextAlpha > 100) throw new IllegalArgumentException("Alpha range should be b/w 0 to 100 (in percentage)"); else { setAlphaOnTextColor(); } } setText(getText()); } void setAlphaOnTextColor() { int alpha = ((255 * mTextAlpha) / 100); setTextColor(getTextColors().withAlpha(alpha)); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (isSquare) { int width = this.getMeasuredWidth(); int height = this.getMeasuredHeight(); int size = Math.max(width, height); int widthSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY); int heightSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY); super.onMeasure(widthSpec, heightSpec); } } }

necesita llamar a super.onMeasure () nuevamente con la especificación EXACTA y el tamaño calculado, ya que setMeasureDimension () parece estar ignorando la gravedad.