[英]Custom Toast in Jetpack Compose
我想要在 Jetpack Compose 中自定義 Toast,但沒有找到任何有關此的有用文檔,將不勝感激
在版本30
(R) 之前,可以使用setView
方法。 如此處所述,現在要么使用標准Toast
要么去Snackbar
Toast
-> setView
/**
* Set the view to show.
*
* @see #getView
* @deprecated Custom toast views are deprecated. Apps can create a standard text toast with the
* {@link #makeText(Context, CharSequence, int)} method, or use a
* <a href="{@docRoot}reference/com/google/android/material/snackbar/Snackbar">Snackbar</a>
* when in the foreground. Starting from Android {@link Build.VERSION_CODES#R}, apps
* targeting API level {@link Build.VERSION_CODES#R} or higher that are in the background
* will not have custom toast views displayed.
*/
@Deprecated
public void setView(View view) {
mNextView = view;
}
不推薦使用自定義 Toast 以支持 SnackBar。
所以他們大多不會在 Jetpack Compose 中獲得支持。
參考。
StackOverflow Post-自定義 Toast 已棄用。
Android 代碼更改棄用自定義 Toast 並附有說明
如上所述棄用自定義 Toast,原因是:
- 出於安全原因,我們正在阻止后台自定義 Toast(go/toast-abuse)。
- 這意味着只有當應用程序在前台時才能進行自定義 Toast。 在前台,應用程序可以控制自己的視圖層次結構,並且能夠創建任何它本來會使用自定義 Toast 的視覺元素。
- 如果我們聲明對前台 Toast 的持續支持,開發人員將處於這樣一種情況:他們要么在發布自定義 Toast 之前檢查應用程序的前台狀態,要么接受他們想要顯示的信息可能根本不會顯示。 這不是很好。
- 還希望完全避免自定義 toast,因為它們會損害 UX 一致性( https://docs.google.com/presentation/d/1r5WEofZ_G3B9M65nS37uD4RqA4iV9HUmngyE6ZpBSsw/edit#slide=id.g7b69852da2_0_0 )。
還添加了在應用程序處於前台時使用 Snackbars 的建議。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.