将图像添加到Toast?

Pra*_*mar 61 android android-toast

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

Par*_*ani 85

是的,您可以使用setView()方法将imageview或任何视图添加到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>
Run Code Online (Sandbox Code Playgroud)

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();
Run Code Online (Sandbox Code Playgroud)


Sam*_*awy 23

简单地说,使用以下内容:

Toast toast = new Toast(myContext);
ImageView view = new ImageView(myContext); 
view.setImageResource(R.drawable.image_icon); 
toast.setView(view); 
toast.show();
Run Code Online (Sandbox Code Playgroud)


Jus*_*ler 14

您可以以编程方式创建任何视图(因为我假设您正在询问如何在不使用LayoutInflater的情况下执行此操作)并在您创建的Toast上调用setView.

    //Create a view here
    LinearLayout v = new LinearLayout(this);
    //populate layout with your image and text or whatever you want to put in here

    Toast toast = new Toast(getApplicationContext());
    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
    toast.setDuration(Toast.LENGTH_LONG);
    toast.setView(v);
    toast.show();
Run Code Online (Sandbox Code Playgroud)


小智 12

Knickedi的解决方案很好,但是如果您只需要文本旁边的图标,您可以利用Toast具有相同ID的预定义TextView并在TextView上设置图标的事实:

Toast toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
TextView tv = (TextView) toast.getView().findViewById(android.R.id.message);
if (null!=tv) {
    tv.setCompoundDrawablesWithIntrinsicBounds(icon, 0, 0, 0);
    tv.setCompoundDrawablePadding(context.getResources().getDimensionPixelSize(R.dimen.padding_toast));
Run Code Online (Sandbox Code Playgroud)