public void showToast(){ //获取一个Toast对象,为下面操做准备 Toast toast = new Toast(this); ImageView img = new ImageView(this); //用系统提供的图片 img.setImageResource(R.drawable.ic_launcher); //设置图片 toast.setView(img); toast.show(); }
最后给一个按钮设定一个监听器,在onClick方法中调用对应的showToast方法就能够了。(下面两个例子一样省略这一步)

public void showToast2(){ Toast toast = Toast.makeText(this, "这是一个有图片的吐司", Toast.LENGTH_LONG); ImageView img = new ImageView(this); img.setImageResource(R.drawable.ic_launcher); //获得toast的布局对象 LinearLayout toast_layout = (LinearLayout) toast.getView(); //为toast添加图片资源,第二个参数,0表示图片在上 toast_layout.addView(img,1); toast.show(); }

有时候上面两种还没能知足本身的要求,就能够自定义布局(我在drawable中放了两张图片,詹姆斯和库里的)android
准备好你想要展现的Toast布局文件,我在layout文件夹新建了一个toast.xml布局
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="horizontal"> 6 <ImageView 7 android:layout_width="50dp" 8 android:layout_height="90dp" 9 android:background="@drawable/c2" 10 /> 11 <TextView 12 android:layout_width="wrap_content" 13 android:layout_height="90dp" 14 android:gravity="center" 15 android:text="VS" 16 /> 17 <ImageView 18 android:layout_width="50dp" 19 android:layout_height="90dp" 20 android:background="@drawable/c1" 21 /> 22 </LinearLayout>
public void showMyTosat(){ //把一个布局变成一个View对象 LayoutInflater inflater = LayoutInflater.from(this); View toast_layout = inflater.inflate(R.layout.toast, null); Toast toast = new Toast(this); //把获取到的View对象做为setView的参数 toast.setView(toast_layout); toast.show(); }
