设置包含文本的自定义视图的样式或字体,例如默认的 TextView?

发布于 2024-10-29 09:36:38 字数 416 浏览 3 评论 0原文

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

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

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

请帮忙!

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遗失的美好 2024-11-05 09:36:38
 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);
    }

}

现在在您的 class/xml 中使用 CustomText

希望这会对您有所帮助。

 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.

虫児飞 2024-11-05 09:36:38

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文