开发者

Setting up the style or typeface of a custom view which contains text, like default TextView?

开发者 https://www.devze.com 2023-02-22 09:29 出处:网络
I`m implementing a custom view which contains text, and the text will be drawn in onDraw. My problem is, I wish my text will looks like those in the TextView objects, but I`m not quit familiar with s

I`m implementing a custom view which contains text, and the text will be drawn in onDraw.

My problem is, I wish my text will looks like those in the TextView objects, but I`m not quit familiar with setting up the style and typeface.

The text finally drawn with my fresh new Paint object, looks different from TextView: The lines looks thinner and seems bitten by some insects... - -b. I wish my text will be drawn just like those TextView objects.

Plz help!

====================

The problem in other words:

In a custom view extending View, I call mPaint.drawText in the onDraw method, with a new Paint object mPaint. But the tex开发者_StackOverflowt drawn looks different to the default TextView, the lines are thinner and not smooth(like were bitten by some insects). I just want it to be the same look as TextView.


 public class CustomText extends TextView { 
 public CustomText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
    Typeface typeface = Typeface.createFromAsset(context.getAssets(),
    "fonts/your_font.ttf");
    setTypeface(typeface);

}

public CustomText(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
    Typeface typeface = Typeface.createFromAsset(context.getAssets(),
    "fonts/fonts/your_font.ttf");
    setTypeface(typeface);

}

public CustomText(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
Typeface typeface = Typeface.createFromAsset(context.getAssets(),
"fonts/fonts/your_font.ttf");
setTypeface(typeface);
    }

}

Now use the CustomText in your class/xml.

Hope this will help you.


You can create a custom view by extending TextView as a parent view. by doing this you will get all the default features that textView has and plus you can add other custom features as per your requirements.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号