问题
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 text 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.
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/5536434/setting-up-the-style-or-typeface-of-a-custom-view-which-contains-text-like-defa