Android 更改 Toast 字体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2940465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change Toast Font
提问by Ye Lin Aung
Currently, I'm trying to develop an app. and I don't know how to change the Toast font. .
目前,我正在尝试开发一个应用程序。而且我不知道如何更改 Toast 字体。.
final OnClickListener clickListener = new OnClickListener() {
public void onClick(View v) {
try {
Toast.makeText(nova.this,"Hello", 500000).show();
}
catch (Exception e) {
Toast.makeText(nova.this,"Exception:" +e, 500000);
}
}
};
I want to change the text "Hello" with custom font I've tried with TypeFace.
我想用我在 TypeFace 上尝试过的自定义字体更改文本“Hello”。
and Then, I want to set a variable at the place "TextClicked" .. I've tried with a local variable .. but it doesn't work
然后,我想在“TextClicked”位置设置一个变量..我试过使用局部变量..但它不起作用
any help with example source code will be really great for me.
示例源代码的任何帮助对我来说都非常有用。
采纳答案by RoflcoptrException
From the official documentation:
来自官方文档:
If a simple text message isn't enough, you can create a customized layout for your toast notification. To create a custom layout, define a View layout, in XML or in your application code, and pass the root View object to the
setView(View)
method.
如果简单的文本消息还不够,您可以为 Toast 通知创建自定义布局。要创建自定义布局,请在 XML 或应用程序代码中定义一个 View 布局,并将根 View 对象传递给该
setView(View)
方法。
Following the link to the official Google Documentation will provide examples.
按照 Google 官方文档的链接将提供示例。
回答by DritanX
The answer is here: https://stackoverflow.com/a/13231981
答案在这里:https: //stackoverflow.com/a/13231981
After refactoring a little:
稍微重构之后:
Toast toast = Toast.makeText(context, R.string.yummyToast, Toast.LENGTH_SHORT); LinearLayout toastLayout = (LinearLayout) toast.getView(); TextView toastTV = (TextView) toastLayout.getChildAt(0); toastTV.setTextSize(30); toast.show();
This worked for me like a charm!
这对我来说就像一个魅力!
回答by hamidfzm
You can use a SpannableString to set the font:
您可以使用 SpannableString 来设置字体:
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/ATaha.ttf");
SpannableString efr = new SpannableString("Toast font changed!");
efr.setSpan(new TypefaceSpan(font), 0, efr.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Toast.makeText(this, efr, Toast.LENGTH_SHORT).show();
A custom Span class that has a specific Typeface set:
具有特定字体集的自定义 Span 类:
public class TypefaceSpan extends MetricAffectingSpan {
private Typeface mTypeface;
public TypefaceSpan(Typeface typeface) {
mTypeface = typeface;
}
@Override
public void updateMeasureState(TextPaint p) {
p.setTypeface(mTypeface);
p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
@Override
public void updateDrawState(TextPaint tp) {
tp.setTypeface(mTypeface);
tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
}
回答by MontyThreeCard
Unfortunately the code on the Java page is bugged. Here is a link to a working function you can implement that gives you the text (I know, because I tested it), and with a little ingenuity, could be expanded to pass arguments for size, color, etc...
不幸的是,Java 页面上的代码被窃听了。这是一个指向您可以实现的工作功能的链接,该功能为您提供文本(我知道,因为我测试过它),并且可以扩展以传递大小,颜色等参数......
Toast Font size function here
Toast 字体大小功能在这里
回答by naXa
Kotlin function:
科特林功能:
fun makeLargeTextToast(text: CharSequence): Toast {
return Toast.makeText(applicationContext, text, Toast.LENGTH_LONG).also {
val toastLayout = it.view as LinearLayout
val toastTV = toastLayout.getChildAt(0) as TextView
toastTV.textSize = 30f
}
}
Use it as:
将其用作:
makeLargeTextToast("text message").show()