如何创建CustomTextView类的对象

时间:2017-10-19 07:00:07

标签: android textview android-canvas

如何创建类的对象,并为AttributeSet等构造函数提供值以及setText(“”)

public class CustomTextView extends AppCompatTextView {

   public CustomTextView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
        textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setTextSize(getTextSize());
    }

    @Override
    public void onDraw(Canvas canvas) {
        TextPaint textPaint = getPaint();
       // ..........
    }
}

//如何在java中使用

CustomTextView cTV = new CustomTextView (...........)??
cTV.setText("how to create like that");

2 个答案:

答案 0 :(得分:2)

首先,您需要定义该类。

public class CustomFontTextView extends AppCompatTextView {

private static final String CUSTOM_FONT = "cfont";
private static final String FONT_PATH = "fonts/";

private String ttfName;
private Typeface font;
private CharSequence text;
private BufferType type;

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

public CustomFontTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init(context, attrs);
}


private void init(Context context, AttributeSet attrs) {

    this.ttfName = attrs.getAttributeValue(AppConstants.NAMESPACE, CUSTOM_FONT);

    if (ttfName.startsWith("@string/") || ttfName.startsWith("@")) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomFontTextView);
        this.ttfName = ta.getString(R.styleable.CustomFontTextView_cfont);
        ta.recycle();
    }

    this.setPaintFlags(this.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);

    try {
        font = Typeface.createFromAsset(context.getAssets(), FONT_PATH + ttfName);
        setTypeface(font);
    } catch (Exception e) {
        e.printStackTrace();
        font = Typeface.defaultFromStyle(Typeface.NORMAL);
        setTypeface(font);
    }

    setText(text, type);
}


@Override
public void setText(CharSequence text, BufferType type) {
    try {
        this.text = text;
        this.type = type;
        if (font == null)
            return;

        CustomFontStyling customFontStyling = new CustomFontStyling(getContext(), font);
        super.setText(customFontStyling.getCustomText(text.toString()), type);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

}

需要在attrs.xml中添加样式后

 <declare-styleable name="CustomFontTextView">
    <attr name="cfont" format="string" />
</declare-styleable>

最后是你的xml布局

 <YOURAPPPACKAGE.CustomFontTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cfont="@string/open_sans_regular" />

答案 1 :(得分:0)

我只是为CustomTextView类创建构造函数

 public CustomTextView(Context context) {
        this(context, null);

 }

现在我们可以轻松访问CustomTextView Class的任何属性。