android - 向 Toast 添加图像?

标签 android android-toast

是否可以以编程方式将图像添加到 toast 弹出窗口?

最佳答案

是的,您可以使用 setView() 方法将 imageview 或任何 View 添加到 toast 通知中,使用此方法您可以根据需要自定义 Toast。

在这里,我创建了一个自定义布局文件以填充到 Toast 通知中,然后我通过 setView() 方法在 Toast 通知中使用了此布局。

cust_toast_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/relativeLayout1"
  android:background="@android:color/white">

    <TextView
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/textView1" android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="PM is here"
        android:gravity="center"
        android:textColor="@android:color/black">
    </TextView>

    <ImageView
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:src="@drawable/new_logo"
        android:layout_below="@+id/textView1"
        android:layout_margin="5dip"
        android:id="@+id/imageView1">
    </ImageView>

    <TextView
        android:id="@+id/textView2"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="This is the demo of Custom Toast Notification"
        android:gravity="center"
        android:layout_below="@+id/imageView1"
        android:textColor="@android:color/black">
    </TextView>

</RelativeLayout>

CustomToastDemoActivity.java

LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.cust_toast_layout, 
    (ViewGroup)findViewById(R.id.relativeLayout1));

Toast toast = new Toast(this);
toast.setView(view);
toast.show();

关于android - 向 Toast 添加图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7571917/

相关文章:

android开发+使用 Intent 在 Activity 之间传输数据

java - 如何在 toast 中使用 double 和/或整数?

java - Android 应用 Toast 混淆

android - AWS Lambda : errorMessage Process exited before completing request

java - 无法从数据库获取上下文和访问架构

滚动时Android背景ListView变黑

java - 在未附加到 Activity 的 Fragment 中调用 Activity 方法

android - Toast.makeText().show() 消息在 Activity-less 服务中去哪里?

安卓 : Floating Action Bar on the Top of Bottom Navigation is Not Working

Android HDPI 大小等于 iOS Retina 大小