为什么当应用程序安装在Android 12 APi 31上时,祝酒词会被截断



文档称,在目标为Android 12或更高版本的应用程序上,Toast被截断为两行。我观察到的行为是,在运行Android 12或更高版本的设备上安装的应用程序上,Toast被截断为两行。

具体来说,我的一个应用程序在我的手机更新到安卓12之前安装了,它的祝酒词不会被截断,但如果我在运行安卓12的模拟器上安装它,它的祝酒词会被截断。在我的手机更新到Android 12后,我重建并安装了另一个应用程序,它的祝酒词被截断了。

更新:

事实上,情况似乎更复杂:行为也取决于设备,显然也取决于它是调试还是发布版本。被截断toast的同一个应用程序在我的手机上用发布版本正确显示了toast,但在模拟器上用相同的发布版本截断了toast。

注意,这与70307699不是同一个问题,其中OP将他的targetSdk更新为31。我的两个应用程序都将targetSdk设置为小于31。

我如何才能得到记录在案的行为并得到我的祝酒词?

安卓平台团队并没有直接贬低祝酒词,而是逐渐降低了祝酒词的用处,这是一种贬低。

https://developer.android.com/reference/android/widget/Toast

最后一句话是:Starting with Android 12 (API level 31), apps targeting Android 12 or newer will have their toasts limited to two lines.

如果我需要多行信息,我会亲自使用Snackbars。

val SNACKBAR_MAX_LINES = 8 // Increase maximum SnackBar line limit above 2
val snackbar = Snackbar.make(bottomNavigationView, "message", Snackbar.LENGTH_SHORT)
(snackbar.view.findViewById(com.google.android.material.R.id.snackbar_text) as TextView)
.run {
maxLines = SNACKBAR_MAX_LINES 
}
snackbar.show()

这可能是一个解决方案。尝试创建自定义toast消息。我试着用PopupWindow创建一个。该类的setContentView方法可以用于设置自定义布局,从而可以支持多行文本。

Android上的自定义吐司:一个简单的示例

如果你想在应用程序的应用程序类中创建一个方法,使其成为通用方法。这是代码。

class ApplicationClass() : Application(), Application.ActivityLifecycleCallbacks {
private var activityReference: Activity? = null
override fun onCreate() {
super.onCreate()
registerActivityLifecycleCallbacks(this)
}
fun showCustomToast(toast: String) {
try {
activityReference?.window?.decorView?.let {
val popupWindow = PopupWindow(it.width - 200, ViewGroup.LayoutParams.WRAP_CONTENT)
val binding = GenericToastPopupBinding.inflate(LayoutInflater.from(this))
binding.toastMessage.text = toast
popupWindow.contentView = binding.root
binding.close.setOnClickListener {
popupWindow.dismiss()
}
popupWindow.setBackgroundDrawable(
ContextCompat.getDrawable(
this,
R.drawable.bg_rectangle_white
)
)
popupWindow.animationStyle = android.R.style.Animation_Toast
popupWindow.showAtLocation(it, Gravity.BOTTOM, 0, 500)
MainScope().launch {
delay(5000)
popupWindow.dismiss()
}
}
} catch (e: Exception) {
Log.d("TAG", "showCustomToast: ")
}
}
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
activityReference = activity
}
override fun onActivityStarted(activity: Activity) {
activityReference = activity
}
override fun onActivityResumed(activity: Activity) {
activityReference = activity
}
override fun onActivityPaused(activity: Activity) {

}
override fun onActivityStopped(activity: Activity) {

}
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {

}
override fun onActivityDestroyed(activity: Activity) {

}
}

相关内容

最新更新