How can we increase the font size in toast?

2019-03-09 05:33发布

Is there any way to increase the font size in toast without customizing?

I don't want to create a layout for increasing the text size.

Is there any way?

Thanks,

Niki

6条回答
2楼-- · 2019-03-09 05:35

I believe it is achieveable by this:

    ViewGroup group = (ViewGroup) toast.getView();
    TextView messageTextView = (TextView) group.getChildAt(0);
    messageTextView.setTextSize(25);
查看更多
Ridiculous、
3楼-- · 2019-03-09 05:40

Here is how to do that with spans:

SpannableStringBuilder biggerText = new SpannableStringBuilder(text);
biggerText.setSpan(new RelativeSizeSpan(1.35f), 0, text.length(), 0);
Toast.makeText(context, biggerText, Toast.LENGTH_LONG).show();
查看更多
够拽才男人
4楼-- · 2019-03-09 05:42

this is ...

 Toast toast = Toast.makeText(context, R.string.yummyToast, Toast.LENGTH_SHORT);
//the default toast view group is a relativelayout
RelativeLayout toastLayout = (RelativeLayout) toast.getView();
TextView toastTV = (TextView) toastLayout.getChildAt(0);
toastTV.setTextSize(30);
toast.show();
查看更多
够拽才男人
5楼-- · 2019-03-09 05:43

You can try to put the following code into your Manifest:

<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true"/> 

Put it above the <Application> element.

查看更多
做个烂人
6楼-- · 2019-03-09 05:45

Working from Ani's answer, another solution that allows you to set the text size to a dimension value would be something like:

public static void showToast(Context context, int resId) {
    Toast toast = Toast.makeText(context, resId, Toast.LENGTH_LONG);
    LinearLayout toastLayout = (LinearLayout) toast.getView();
    TextView toastTV = (TextView) toastLayout.getChildAt(0);
    toastTV.setTextSize(TypedValue.COMPLEX_UNIT_PX,
            context.getResources().getDimension(R.dimen.TEXT_SIZE));
    toast.show();
}

This lets you match the size you get your toasts to be the same size as specified in TextView and Button controls, for example.

查看更多
我欲成王,谁敢阻挡
7楼-- · 2019-03-09 05:51

You can't increase the font size without creating a CustomToastView.

This is a related question.

查看更多
登录 后发表回答