【发布时间】:2011-07-13 13:26:53
【问题描述】:
有什么方法可以在不自定义的情况下增加 toast 的字体大小?
我不想创建用于增加文本大小的布局。
有什么办法吗?
谢谢,
尼基
【问题讨论】:
标签: android text android-layout font-size
有什么方法可以在不自定义的情况下增加 toast 的字体大小?
我不想创建用于增加文本大小的布局。
有什么办法吗?
谢谢,
尼基
【问题讨论】:
标签: android text android-layout font-size
我相信这是可以实现的:
ViewGroup group = (ViewGroup) toast.getView();
TextView messageTextView = (TextView) group.getChildAt(0);
messageTextView.setTextSize(25);
【讨论】:
这是……
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();
【讨论】:
以下是如何使用跨度来做到这一点:
SpannableStringBuilder biggerText = new SpannableStringBuilder(text);
biggerText.setSpan(new RelativeSizeSpan(1.35f), 0, text.length(), 0);
Toast.makeText(context, biggerText, Toast.LENGTH_LONG).show();
【讨论】:
如果不创建CustomToastView,就无法增加字体大小。
This 是一个相关问题。
【讨论】:
根据 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 控件中指定的大小相同。
【讨论】:
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。
【讨论】:
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();
【讨论】:
您可以尝试将以下代码放入您的 Manifest:
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true"/>
将其放在<Application> 元素上方。
【讨论】: