小编use*_*678的帖子

自定义TextView - 在构造函数之前调用setText()

我的CustomTextView有问题.我正在尝试从我的layout-xml文件中获取自定义值并在我的setText()方法中使用它.不幸的是,在构造函数之前调用setText()方法,因此我无法在此方法中使用自定义值.

这是我的代码(细分到相关部分):

CustomTextView.class

public class CustomTextView extends TextView {

    private float mHeight;
    private final String TAG = "CustomTextView";
    private static final Spannable.Factory spannableFactory = Spannable.Factory.getInstance();

    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        Log.d(TAG, "in CustomTextView constructor");
        TypedArray values = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView);
        this.mHeight = values.getDimension(R.styleable.CustomTextView_cHeight, 20);
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        Log.d(TAG, "in setText function");
        Spannable s = getCustomSpannableString(getContext(), text);
        super.setText(s, BufferType.SPANNABLE);
    }

    private static …
Run Code Online (Sandbox Code Playgroud)

android textview android-custom-view

11
推荐指数
1
解决办法
4006
查看次数

标签 统计

android ×1

android-custom-view ×1

textview ×1