我正在实现Square 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中将文本居中呢?我尝试过以文本视图为中心的相对延迟-结果是相同的。请帮忙..

最佳答案

希望你现在已经得到了答案。如果没有,可以使用以下命令:

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

关于android - Android setMeasuredDimension-重力被打破,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20616168/

10-09 07:04