要在Android Toast中显示应用图标,你可以自定义Toast布局,并在布局中添加应用图标。下面是一个示例代码:

  1. 首先,在你的项目的res文件夹下创建一个新的布局文件(例如toast_layout.xml),并添加一个ImageView组件来显示应用图标。例如:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="16dp">

    <ImageView
        android:id="@+id/app_icon"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/text_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFF" />

</LinearLayout>
  1. 在你的Java代码中,使用LayoutInflater加载自定义布局文件,并将其设置为Toast的视图。例如:
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout, findViewById(R.id.toast_layout_root));

ImageView icon = layout.findViewById(R.id.app_icon);
TextView text = layout.findViewById(R.id.text_message);

icon.setImageResource(R.mipmap.ic_launcher);
text.setText("Your message here");

Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);
toast.show();

这样,你就可以在Toast中显示带有应用图标的自定义布局了。记得将R.mipmap.ic_launcher替换为你的应用图标资源

android toast 带应用图标

原文地址: https://www.cveoy.top/t/topic/iMSx 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录