0

我正在实现一个包含文本的自定义视图,文本将在 onDraw 中绘制。

我的问题是,我希望我的文本看起来像 TextView 对象中的那些,但我并不熟悉设置样式和字体。

文本终于用我的新 Paint 对象绘制,看起来与 TextView 不同:线条看起来更细,似乎被一些昆虫咬了...... - -b。我希望我的文本将像那些 TextView 对象一样绘制。

请帮忙!

====================
换句话说问题:

在扩展 View 的自定义视图中,我在 onDraw 方法中调用 mPaint.drawText,并使用新的 Paint 对象 mPaint。但是绘制的文本看起来与默认的 TextView 不同,线条较细且不平滑(就像被一些昆虫咬过一样)。我只是希望它与 TextView 具有相同的外观。

4

2 回答 2

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);
    }

}

现在CustomText在你的类/xml 中使用。

希望这会帮助你。

于 2011-04-04T12:50:40.403 回答
0

您可以通过将 TextView 扩展为父视图来创建自定义视图。通过这样做,您将获得 textView 具有的所有默认功能,并且您可以根据您的要求添加其他自定义功能。

于 2011-04-04T09:55:07.413 回答