如何增加 toast 中的字体大小?

发布于 2024-10-21 14:41:53 字数 100 浏览 7 评论 0原文

有没有办法在不自定义的情况下增加toast中的字体大小?

我不想创建用于增加文本大小的布局。

有什么办法吗?

谢谢,

尼基

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

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

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

发布评论

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

评论(9

俯瞰星空 2024-10-28 14:41:53

我相信这是可以实现的:

    ViewGroup group = (ViewGroup) toast.getView();
    TextView messageTextView = (TextView) group.getChildAt(0);
    messageTextView.setTextSize(25);

I believe it is achieveable by this:

    ViewGroup group = (ViewGroup) toast.getView();
    TextView messageTextView = (TextView) group.getChildAt(0);
    messageTextView.setTextSize(25);
白色秋天 2024-10-28 14:41:53

这是...

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

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();
緦唸λ蓇 2024-10-28 14:41:53

以下是如何使用跨度来做到这一点:

SpannableStringBuilder biggerText = new SpannableStringBuilder(text);
biggerText.setSpan(new RelativeSizeSpan(1.35f), 0, text.length(), 0);
Toast.makeText(context, biggerText, Toast.LENGTH_LONG).show();

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();
征棹 2024-10-28 14:41:53

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

This is a related question.

﹏半生如梦愿梦如真 2024-10-28 14:41:53

根据 Ani 的答案,另一种允许您将文本大小设置为尺寸值的解决方案如下所示:

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

这可以让您将 Toast 的大小与 TextView 和 Button 控件中指定的大小相匹配。

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.

悟红尘 2024-10-28 14:41:53

尼古拉回答得很漂亮。试探性地,可以肯定的是,如果 Android API 方法采用 CharSequence 而不是 String,则意味着您可以传递它 Spanned (即格式化)文本。

如果您不想手动构建它,并且更熟悉 HTML,您可以这样做:

Toast.makeText(context, Html.fromHtml("<big>Big text.</big>"), Toast.LENGTH_SHORT)

或者,如果使用字符串资源,请执行以下操作:

<string name="big_text"><big>Big text.</big></string>

然后像这样使用它:

Toast.makeText(context, R.string.big_text, Toast.LENGTH_SHORT)

可用的标记大多记录在 < a href="https://developer.android.com/guide/topics/resources/string-resource#StylingWithHTML" rel="nofollow noreferrer">此处。

Nikolay answered this beautifully. Heuristically, it's pretty a safe bet that, if an Android API method takes a CharSequence instead of a String it means you can pass it Spanned (i.e. formatted) text.

If you don't want to want to build it by hand, and are more comfortable with HTML you can do this:

Toast.makeText(context, Html.fromHtml("<big>Big text.</big>"), Toast.LENGTH_SHORT)

Or, if using a string resource, do this:

<string name="big_text"><big>Big text.</big></string>

And just use it like this:

Toast.makeText(context, R.string.big_text, Toast.LENGTH_SHORT)

The available markup is mostly documented here.

千纸鹤带着心事 2024-10-28 14:41:53
    Toast toast = Toast.makeText(MyApplication.getContext(), "here", Toast.LENGTH_LONG);
    ViewGroup group = (ViewGroup) toast.getView();
    TextView messageTextView = (TextView) group.getChildAt(0);
    messageTextView.setTextSize(30);
    toast.show();
    Toast toast = Toast.makeText(MyApplication.getContext(), "here", Toast.LENGTH_LONG);
    ViewGroup group = (ViewGroup) toast.getView();
    TextView messageTextView = (TextView) group.getChildAt(0);
    messageTextView.setTextSize(30);
    toast.show();
从来不烧饼 2024-10-28 14:41:53

试试这个:

SpreadsheetApp.getActive().toast(" ","你想要的文本在这里");

通过将第一组引号保留为空,第二组引号会将文本加粗并使其更易于阅读。

通常该行是: .toast("您想要的文本","您的 toast 弹出窗口的标题");

通过将文本设置为标题、第二组引号并将第一组引号留空,您的文本现在更易于阅读。

Try this:

SpreadsheetApp.getActive().toast(" ","The text you want here");

By keeping the first set of quotes empty, the second set will bold your text and make it easier to read.

Normally the line would be: .toast("Text you want here","The title of your toast popup");

By making your text as the Title, second set of quotes, and leaving the first quotes empty, your text is now easier to read.

作死小能手 2024-10-28 14:41:53

您可以尝试将以下代码放入 Manifest 中:

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

将其放在 元素上方。

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.

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