当前位置: 动力学知识库 > 问答 > 编程问答 >

android - how to set typeface for toast?

问题描述:

I am able to set custom font to my text view like this

Typeface typeface = Typeface.createFromAsset(context.getAssets(),"fonts/akshar.ttf");

setTypeface(typeface);

How can I set same thing to default Toast so that I am able to render my locale text in toast messages. I am able to set Gravity, duration but not typeface.

Thanks In Advance.

网友答案:

YOu can Create a custom Toast as:

 LayoutInflater inflater = getLayoutInflater();
    View layout = inflater.inflate(R.layout.toast_layout,
                                   (ViewGroup) findViewById(R.id.toast_layout_root));

    TextView text = (TextView) layout.findViewById(R.id.text);
    text.setText("Hello! This is a custom toast!");
    Typeface typeface = Typeface.createFromAsset(context.getAssets(),"fonts/akshar.ttf");
    text.setTypeface(typeface);
    Toast toast = new Toast(getApplicationContext());
    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
    toast.setDuration(Toast.LENGTH_LONG);
    toast.setView(layout);

toast.show();

and for more details how we create a custom Toast see CustomToastView

网友答案:

Toast has a method setView(), so you can set the typeface for the TextView and add the same TextView to Toast using

toast.setView(textview);
分享给朋友:
您可能感兴趣的文章:
随机阅读: