自定义android方形textview文本不居中

自定义android方形textview文本不居中,第1张

概述我正在尝试创建一个总是平方的textview,所以我已经实现了这个自定义类. public class SquareTextView extends TextView { public SquareTextView(Context context) { super(context); } public SquareTextView(Context con 我正在尝试创建一个总是平方的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>

这是一张图片

这对于使视图平方很有效,然而,似乎重力变得混乱了.有了这个,文本似​​乎总是在左上角.如何才能使TextVIEw始终保持平方但仍保持重力或至少能够使文本居中

编辑:经过一些测试,我注意到如果你将宽度或高度设置为特定的dp大小,重力似乎再次起作用.所以它可能与WRAP_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,attrs);        init(attrs);    }    public TextAlphaSquareTextVIEw(Context context,int defStyle) {        super(context,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,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()似乎忽略了引力.

总结

以上是内存溢出为你收集整理的自定义android方形textview文本不居中全部内容,希望文章能够帮你解决自定义android方形textview文本不居中所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1122757.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存