更改吐司字体

发布于 2024-09-03 04:52:32 字数 529 浏览 9 评论 0原文

目前,我正在尝试开发一个应用程序。 我不知道如何更改 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”位置设置一个变量..我尝试过使用局部变量..但它不起作用,

示例源代码的任何帮助对我来说真的很棒。

Currently, I'm trying to develop an app.
and I don't know how to change the Toast font. .

 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.

and Then, I want to set a variable at the place "TextClicked" .. I've tried with a local variable .. but it doesn't work

any help with example source code will be really great for me.

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

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

发布评论

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

评论(6

奢华的一滴泪 2024-09-10 04:52:32

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

重构一下之后:

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

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

The answer is here: 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!

时常饿 2024-09-10 04:52:32

来自官方文档:

创建自定义 ToastView

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

点击官方 Google 文档的链接将提供示例。

From the official documentation:

Create your custom ToastView

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.

Following the link to the official Google Documentation will provide examples.

晚风撩人 2024-09-10 04:52:32

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

具有特定 Typeface 设置的自定义 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);
    }
}

You can use a SpannableString to set the font:

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:

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);
    }
}
一场春暖 2024-09-10 04:52:32

不幸的是,Java 页面上的代码有错误。这是一个指向您可以实现的工作函数的链接,该函数为您提供文本(我知道,因为我测试了它),并且只需一点​​点独创性,就可以扩展以传递大小、颜色等参数...

Toast Font size函数此处

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

久随 2024-09-10 04:52:32

Kotlin 函数:

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()

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()
挽清梦 2024-09-10 04:52:32

中使用了这个解决方案

我在 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()
}

祝你好运!

I used this solution in kotlin

in CustomView or 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 :

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

used in fragment:

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

good luck!!

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