英文:
Method Toast.setView is deprecated
问题
在我正在我的应用程序上创建自定义提示消息时,我注意到setView
方法已被弃用。
有人对此有解决方案吗?
toast.setView(customView);
英文:
While I'm doing custom toast on my app, I noticed that setView is deprecated.
Does anyone have a solution for this?
toast.setView(customView);
答案1
得分: 12
由于setView
已被弃用:
此方法在API级别30中已被弃用。
自定义Toast视图已被弃用。应用程序可以使用makeText(android.content.Context, java.lang.CharSequence, int)
方法创建标准文本Toast,或者在前台使用Snackbar
。从AndroidBuild.VERSION_CODES#R
开始,针对API级别Build.VERSION_CODES#R
或更高版本的应用程序在后台不会显示自定义Toast视图。
这是有道理的,Toast可以显示在其他应用程序的上方,某些应用程序可以通过在其他应用程序的上方创建自定义Toast来欺骗用户,以获取他们的优势,即使他们的应用程序在后台运行。但如果您的应用程序在前台,您的自定义Toast仍将在所有Android版本中显示。
英文:
Since setView
is deprecated:
> This method was deprecated in API level 30.
Custom toast views are deprecated. Apps can create a standard text toast with the makeText(android.content.Context, java.lang.CharSequence, int)
method, or use a Snackbar
when in the foreground. Starting from Android Build.VERSION_CODES#R
, apps targeting API level Build.VERSION_CODES#R
or higher that are in the background will not have custom toast views displayed.
This makes sense Toasts can be displayed on Top of other Apps, some Apps can trick users by creating custom Toasts on Top of other Apps for their advantage even if their App is on the Background. But if your App is in the Foreground your custom Toast will still be shown in all Android Versions.
答案2
得分: 2
在API 30及更高版本中,使用自定义视图设置Toast的解决方法已被弃用。
文档说明:
在API级别30中,此方法已弃用。自定义Toast视图已被弃用。应用程序可以使用makeText(android.content.Context, java.lang.CharSequence, int)方法创建标准文本Toast,或者在前台时使用Snackbar。从Android Build.VERSION_CODES#R开始,针对API级别Build.VERSION_CODES#R或更高级别的应用程序在后台不会显示自定义Toast视图。
尽管如此,某些情况下仍有解决方法:
Toast.makeText(applicationContext,
HtmlCompat.fromHtml("<font color='red'>custom toast message</font>", HtmlCompat.FROM_HTML_MODE_LEGACY),
Toast.LENGTH_LONG).show()
HTML颜色标签也可以是 <font color='#ff6347'>
对于涉及显示文本的任何修改,上述解决方法已足够。例如,您可以通过插入 <b>my text</b>
使文本加粗,或者可能希望使用 <font font-family='...'> my text </font>
更改字体族。对于所有这些更改,该解决方法都足够。
但是,如果您想要修改容器的属性,如背景颜色,唯一的替代方法是使用Snackbar。无法再为Toast修改视图。
英文:
The solution with setting a custom view on Toast is deprecated for API 30 and forward.
Documentation says
> This method was deprecated in API level 30. Custom toast views are
> deprecated. Apps can create a standard text toast with the
> makeText(android.content.Context, java.lang.CharSequence, int) method,
> or use a Snackbar when in the foreground. Starting from Android
> Build.VERSION_CODES#R, apps targeting API level Build.VERSION_CODES#R
> or higher that are in the background will not have custom toast views
> displayed.
There is a walkaround for some cases though
Toast.makeText(applicationContext,
HtmlCompat.fromHtml("<font color='red'>custom toast message</font>", HtmlCompat.FROM_HTML_MODE_LEGACY),
Toast.LENGTH_LONG).show()
Html color tag can also be <font color='#ff6347'>
For every modification that has to do with the displayed text, the above solution would be enough. You can for example make the text bold by inserting <b>my text</b>
or you maybe want to change the font-family with <font font-family='...'> my text </font>
For all those changes that solution will be enough.
If you want to modify the container though with properties like background-color the only alternative is to use Snackbar. View can not be modified for Toast anymore.
答案3
得分: 1
正如其他答案已经提到的原因和使用 snackbar/默认 toast,我将提供我使用的替代方法。
我们可能无法自定义 toast 的背景,但我们可以使用 Spannable string 来自定义 toast 中显示的文本。默认 toast 背景将会显示,但是使用 android.text.style 包下可用的不同 span 样式,我们可以在 toast 消息中实现自定义文本样式。
示例自定义 toast,其中显示文本颜色为绿色,文本大小为 200 像素。
val spannableString = SpannableString("自定义 toast")
spannableString.setSpan(
ForegroundColorSpan(Color.GREEN), 0, spannableString.length, 0
)
spannableString.setSpan(
AbsoluteSizeSpan(200), 0, spannableString.length, 0
)
val toast = Toast.makeText(context, spannableString, Toast.LENGTH_SHORT)
toast.show()
Spannable string 参考:spantastic 使用 spans 进行文本样式设置
(附注:当应用程序正在运行时,我们始终可以显示自定义对话框或自定义通知,以向用户显示重要消息。)
英文:
As other answers already mentioned the reasons and to use snackbar/deafult toast, I will provide the alternative I use.
We may not able to customise the toast background but we can use Spannable string to customise the text displayed in the toast. The default toast background will be shown but using different span styles available under package: android.text.style, we can achieve custom text style in the toast message.
Example custom toast which shows toast with text color in green and text size of 200 pixels.
val spannableString = SpannableString("Custom toast")
spannableString.setSpan(
ForegroundColorSpan(Color.GREEN), 0, spannableString.length, 0
)
spannableString.setSpan(
AbsoluteSizeSpan(200), 0, spannableString.length, 0
)
val toast = Toast.makeText(context, spannableString, Toast.LENGTH_SHORT)
toast.show()
Spannable string reference: spantastic text styling with spans
(PS: We could always show custom dialogs when app is running or custom notifications to show important messages to the users.)
答案4
得分: 0
我编写了一个简短的Kotlin扩展函数,使用spannable来实现这一功能。请注意,它返回一个需要“显示”的toast。
fun Context.spToPix(sp: Int): Int =
(sp * Resources.getSystem().displayMetrics.scaledDensity).toInt()
fun Context.fontSizeToast(
fontSize: Int,
mess: String,
displayTime: Int = Toast.LENGTH_LONG
): Toast {
val ssMess = SpannableString(mess)
ssMess.setSpan(
AbsoluteSizeSpan(spToPix(fontSize)),
0, ssMess.length, 0
)
return Toast.makeText(this, ssMess, displayTime)
}
英文:
I wrote a short Kotlin extension function to do this using spannable. Note that it returns a toast that you need to "show".
fun Context.spToPix(sp: Int): Int =
(sp * Resources.getSystem().displayMetrics.scaledDensity).toInt()
fun Context.fontSizeToast(
fontSize: Int,
mess: String,
displayTime: Int = Toast.LENGTH_LONG
): Toast {
val ssMess = SpannableString(mess)
ssMess.setSpan(
AbsoluteSizeSpan(spToPix(fontSize)),
0, ssMess.length, 0
)
return Toast.makeText(this, ssMess, displayTime)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论