note of book Android Programming

《Android 编程权威指南 第3版》笔记。

1.11 挑战练习:定制 toast 消息

1
2
3
Toast t = Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT);
t.setGravity(Gravity.TOP,0,0);
t.show();

Android R(API 30) 之后 setGravity() 对 text 类型的 Toast 不起作用。

Warning: Starting from Android Build.VERSION_CODES#R, for apps targeting API level Build.VERSION_CODES#R or higher, this method is a no-op when called on text toasts.
https://developer.android.com/reference/android/widget/Toast#setGravity(int,%20int,%20int)

在 logcat 中可看到如下错误:

1
E/Toast: setGravity() shouldn't be called on text toasts, the values won't be used

将 targetSdkVersion 改为 29 可暂时解决该问题[Doge]。