【问题标题】:How can we increase the font size in toast?我们如何增加吐司的字体大小?
【发布时间】:2011-07-13 13:26:53
【问题描述】:

有什么方法可以在不自定义的情况下增加 toast 的字体大小?

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

有什么办法吗?

谢谢,

尼基

【问题讨论】:

    标签: android text android-layout font-size


    【解决方案1】:

    我相信这是可以实现的:

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

    【讨论】:

    • 此代码因某些自定义操作系统设备中的 ClassCastException 而崩溃。 java.lang.ClassCastException android.widget.RelativeLayout 无法转换为 android.widget.LinearLayout 在这种情况下,似乎 toast 视图是使用 RelativeLayout 父级实现的。可以通过类型转换为 ViewGroup 而不是 LinearLayout 来修复。
    【解决方案2】:

    这是……

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

      • 谢谢,完美的解决方案,我可以期待吗?所以,这段代码运行得很好。
      • 谢谢!工作正常
      【解决方案4】:

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

      This 是一个相关问题。

      【讨论】:

      • 谢谢.. 但即使是 CustomToastView 在我的服务中也不起作用。我们应该在哪里做充气部分(onCreate 或 onStartCommand )?我尝试了 onCreate,但以错误结束。
      【解决方案5】:

      根据 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 控件中指定的大小相同。

      【讨论】:

        【解决方案6】:

        Nikolay 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

        【讨论】:

          【解决方案7】:
              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();
          

          【讨论】:

            【解决方案8】:

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

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

            将其放在&lt;Application&gt; 元素上方。

            【讨论】:

            • 这与实际问题有什么关系?
            • 我猜这是一个应用程序范围的解决方案。如果 Android 识别出设备具有大屏幕,它可能会显示更大的 toast。
            猜你喜欢
            • 2011-04-08
            • 2013-04-03
            • 2017-10-07
            • 2014-09-22
            • 2012-11-07
            • 1970-01-01
            • 1970-01-01
            • 2015-12-05
            • 2012-08-11
            相关资源
            最近更新 更多