安卓setMeasuredDimension - 布局位置失效

3

我正在实现正方形TextView并且无法居中其文字。

以下是正方形效果的代码:

public class SquareTextView extends TextView {

public SquareTextView(Context context) {
    super(context);
}

public SquareTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    setMeasuredDimension(getMeasuredHeight(), getMeasuredHeight());
}

}

以下是我的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp" >

<SquareTextView
    android:id="@+id/squareTextView1"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="center"
    android:background="@android:color/darker_gray"
    android:text="47"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="8dp"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
</LinearLayout>

最终我得到了这个:

在此输入图片描述

那么,我应该如何将文本居中在TextView中?我尝试使用RelativeLayout并将TextView居中-结果仍然相同。请帮帮我。

1个回答

1

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接