如何修改Android 11 Toast中显示的蓝色默认安卓占位图标
问题原因
你提到的测试环境API 31对应Android 12,而非Android 11。从Android 12(API 31)开始,系统为了明确Toast的来源,默认会在文本Toast左侧自动添加应用图标,该特性不会在更低版本系统上触发,和你当前的Toast调用方式无关。
解决方案
方案1:替换为自定义图标
系统默认Toast读取的是你的AndroidManifest.xml中<application>标签配置的android:icon属性对应的图标,按以下步骤检查修改即可:
- 确认
<application>标签的android:icon属性指向的是你自定义的图标资源,而非系统默认占位图 - 如果配置了自适应图标/圆形图标,同步检查
android:roundIcon、mipmap-anydpi-v26下的自适应图标配置是否都替换为了自定义资源 - 修改完成后重新编译运行,默认Toast的图标就会自动替换为你的应用图标
方案2:完全移除Toast图标
如果不需要显示图标,可以通过以下两种方式实现:
方式A:修改系统默认Toast(适配API 30+)
无需自定义布局,直接在Toast显示前隐藏图标控件即可:
Toast toast = Toast.makeText(getContext(), getString(R.string.crashTurnedOn), Toast.LENGTH_SHORT); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { toast.addCallback(new Toast.Callback() { @Override public void onToastShown() { super.onToastShown(); ImageView toastIcon = toast.getView().findViewById(android.R.id.icon); if (toastIcon != null) { toastIcon.setVisibility(View.GONE); } } }); } toast.show();
方式B:自定义Toast布局(全版本适配,兼容性最好)
如果遇到定制ROM修改了系统Toast布局导致方式A失效,可以完全自定义Toast样式:
- 新建布局文件
res/layout/custom_toast.xml,自行定义样式:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/toast_bg" android:padding="16dp" android:orientation="horizontal"> <TextView android:id="@+id/tv_toast_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/white" android:textSize="14sp"/> </LinearLayout>
- 调用自定义Toast:
View customView = LayoutInflater.from(getContext()).inflate(R.layout.custom_toast, null); TextView tvContent = customView.findViewById(R.id.tv_toast_content); tvContent.setText(R.string.crashTurnedOn); Toast customToast = new Toast(getContext()); customToast.setView(customView); customToast.setDuration(Toast.LENGTH_SHORT); customToast.show();
内容的提问来源于stack exchange,提问作者J34245
相关产品推荐
相关产品推荐

