自定义android方形文本视图文本未居中

自定义android方形文本视图文本未居中,android,textview,android-custom-view,Android,Textview,Android Custom View,我正在尝试创建一个总是平方的textview,所以我实现了这个自定义类 public class SquareTextView extends TextView { public SquareTextView(Context context) { super(context); } public SquareTextView(Context context, AttributeSet attrs) { super(context, at

我正在尝试创建一个总是平方的textview,所以我实现了这个自定义类

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);
    }

}
下面是一个说明问题的布局示例:

<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>

这是一张这样的照片

这在将视图平方化方面非常有效,但是,似乎重力被弄乱了。这样,文本似乎总是在左上角。我怎样才能拥有一个文本视图,它始终是正方形的,但仍然保持重力,或者至少能够使文本居中


编辑:经过一些测试后,我注意到如果将宽度或高度设置为特定的dp大小,重力似乎又起作用了。因此,它可能与
WRAP\u CONTENT
属性有关。在onmeasure方法中是否会以另一种方式处理该问题,从而导致我自己的方法无法按预期工作?

希望您现在已经得到了答案。如果没有,您可以使用以下选项:

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);
        }
    }
}
公共类TextAlphaSquareTextView扩展了AppCompatTextView{
私有int mTextAlpha=0;
私有布尔值isSquare=false;
公共文本AlphaSquareTextView(上下文){
超级(上下文);
初始化(空);
}
公共文本AlphaSquareTextView(上下文、属性集属性){
超级(上下文,attrs);
init(attrs);
}
公共文本AlphaSquareTextView(上下文上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
init(attrs);
}
私有void init(属性集属性){
if(attrs==null){
}否则{
TypedArray a=getContext().ActainStyledAttributes(attrs,R.styleable.TextAlphaSquareTextView);
mTextAlpha=a.getInteger(R.styleable.TextAlphaSquareTextView_textAlpha,100);
isSquare=a.getBoolean(R.styleable.TextAlphaSquareTextView_squareMode,false);
a、 回收();
如果(mTextAlpha<0 | | mTextAlpha>100)
抛出新的IllegalArgumentException(“Alpha范围应为b/w 0到100(百分比)”;
否则{
setAlphaContextColor();
}
}
setText(getText());
}
void setAlphaContextColor(){
int alpha=((255*mTextAlpha)/100);
setTextColor(getTextColors().withAlpha(alpha));
}
@凌驾
测量时的保护空隙(内部宽度测量等级、内部高度测量等级){
超级测量(宽度测量、高度测量);
如果(isSquare){
int width=this.getMeasuredWidth();
int height=this.getMeasuredHeight();
int size=数学最大值(宽度、高度);
int-widthSpec=MeasureSpec.makeMeasureSpec(大小、测量精度);
int heightSpec=MeasureSpec.makeMeasureSpec(尺寸,精确测量);
超级测量(宽度规格、高度规格);
}
}
}

您需要再次调用super.onMeasure(),使用精确的规格和计算的大小,因为setMeasureDimension()似乎忽略了重力。

您是否尝试将添加到xml:
android:gravity=“center”
中,或者您也可以通过java代码修改重力,以便在默认情况下为自定义view@LucasCrawford对我已经用布局XML更新了我的问题,正如您所看到的,我已经设置了重力。