21

目前,我正在尝试开发一个应用程序。而且我不知道如何更改 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);
            }
        }
    };

我想用我用 TypeFace 尝试过的自定义字体更改文本“Hello”。

然后,我想在“TextClicked”位置设置一个变量..我尝试过使用局部变量..但它不起作用

任何有关示例源代码的帮助对我来说都非常有用。

4

6 回答 6

34

答案在这里:https ://stackoverflow.com/a/13231981

稍作重构后:

    Toast toast = Toast.makeText(context, R.string.yummyToast, Toast.LENGTH_SHORT);
    线性布局 toastLayout = (LinearLayout) toast.getView();
    TextView toastTV = (TextView) toastLayout.getChildAt(0);
    toastTV.setTextSize(30);
    toast.show();

这对我来说就像一个魅力!

于 2013-03-29T18:16:47.067 回答
19

来自官方文档:

创建您的自定义 ToastView

如果简单的短信还不够,您可以为 Toast 通知创建自定义布局。要创建自定义布局,请在 XML 或应用程序代码中定义视图布局,并将根视图对象传递给setView(View)方法。

按照官方 Google 文档的链接将提供示例。

于 2010-05-30T22:01:52.990 回答
10

您可以使用 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();

具有特定字体集的自定义 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);
    }
}
于 2014-05-13T16:43:31.273 回答
3

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...

Toast Font size function here

于 2013-02-20T20:07:23.187 回答
1

科特林功能:

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

将其用作:

makeLargeTextToast("text message").show()
于 2019-03-26T14:18:07.863 回答
0

我在 kotlin 中使用了这个解决方案

在 CustomView 或 Fragment

 fun persianToast(message: String): Toast {
    return Toast.makeText(context, message, Toast.LENGTH_SHORT).also {
        val view = it.view as LinearLayout
        val tv = view.getChildAt(0) as TextView
        val typeFace = Typeface.createFromAsset(context?.assets, MyApplication.getFont(MyApplication.LIGHT_FONT))
        tv.typeface = typeFace
    }
 }

MyApplication 类:

class MyApplication : Application() {
companion object {
     const val NORMAL_FONT = 0
     const val BOLD_FONT = 1
     const val MEDIUM_FONT = 2
     const val LIGHT_FONT = 3
     const val ULTRA_LIGHT_FONT = 4

    @JvmStatic
    fun getFont(type: Int): String {
        return when (type) {
            LIGHT_FONT -> "font/fontLight.ttf"
            BOLD_FONT -> "font/fontBold.ttf"
            MEDIUM_FONT -> "font/fontMedium.ttf"
            ULTRA_LIGHT_FONT -> "font/fontUltraLight.ttf"
            else -> "font/fontNormal.ttf"
        }
    }
}

在片段中使用:

  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        Toast(context)
        persianToast("javid sattar").show()
}

祝你好运!!

于 2020-09-09T06:58:56.533 回答