我们怎样才能增加吐司的字体大小?

时间:2011-03-11 14:46:13

标签: android text android-layout font-size

有没有办法在没有自定义的情况下增加吐司的字体大小?

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

有什么办法吗?

谢谢,

尼基

8 个答案:

答案 0 :(得分:52)

我相信这是可以实现的:

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

答案 1 :(得分:26)

这是......

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

答案 2 :(得分:11)

以下是使用跨度的方法:

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

答案 3 :(得分:8)

如果不创建CustomToastView,则无法增加字体大小。

This是一个相关的问题。

答案 4 :(得分:1)

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

这使您可以将获得吐司的大小与TextView和Button控件中指定的大小相匹配,例如。

答案 5 :(得分:1)

尼古拉answered这很漂亮。启发式地,可以肯定的是,如果 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)

可用标记主要记录在 here 中。

答案 6 :(得分:0)

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

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

将其置于<Application>元素上方。

答案 7 :(得分:0)

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