【问题标题】:Custom android square textview text is not centered自定义android方形textview文本不居中
【发布时间】:2016-08-31 18:24:48
【问题描述】:

我正在尝试创建一个始终为平方的文本视图,因此我实现了这个自定义类。

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>

这是一张图片

这在使视图平方方面效果很好,但是,似乎重力被弄乱了。这样,文本似乎总是在左上角。我怎样才能拥有一个始终是平方但仍保持重力或至少能够使文本居中的 TextView?

编辑:经过一些测试,我注意到如果您将宽度或高度设置为特定的 dp 大小,重力似乎再次起作用。所以它可能与WRAP_CONTENT 属性有关。是否会在 onmeasure 方法中以另一种方式处理这可能导致我自己的方法无法按预期工作?

【问题讨论】:

  • 您是否尝试过添加到 xml:android:gravity="center" 或者您也可以通过 java 代码修改重力,以便默认为自定义视图执行此操作
  • @LucasCrawford 是的,我已经用布局 XML 更新了我的问题,你可以看到我已经设置了重力。

标签: android textview android-custom-view


【解决方案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);
        }
    }
}

您需要使用 EXACT 规格和计算出的尺寸再次调用 super.onMeasure(),因为 setMeasureDimension() 似乎忽略了重力。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-18
    • 2012-02-29
    • 2016-08-18
    • 2014-02-14
    • 1970-01-01
    • 1970-01-01
    • 2018-11-15
    • 2014-01-09
    相关资源
    最近更新 更多