0

如何将自定义图像添加到自定义 Toast...

4

3 回答 3

1

只需将 ImageView 添加到您的 toast_layout 中,然后:

ImageView iv=  (ImageView) layout.findViewById(R.id.image);
iv.setBackgroundResource(R.drawable.img1);//Or wathever you want!

这将起作用。

于 2013-03-01T10:10:17.387 回答
0
03-01 15:55:41.040: E/AndroidRuntime(6512): java.lang.NullPointerException
03-01 15:55:41.040: E/AndroidRuntime(6512):    at com.anthem.mathsmagic.maxone$1.onClick(maxone.java:98)

这是 onClick 方法中相当常见的 NPE。你在第 98 行做什么?

于 2013-03-01T10:28:11.757 回答
-2

我是这样做的:

LayoutInflater inflater = LayoutInflater.from(getActivity());
    View layout = inflater.inflate(R.layout.toast, null); 
     ImageView image = (ImageView) layout.findViewById(R.id.image); 
     image.setImageBitmap(bm);

    TextView text = (TextView) layout.findViewById(R.id.toast_text);
    String toastText = "Hi"; 
    text.setText(toastText);

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

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/covu_sdk_background_toast"
android:orientation="horizontal"
android:padding="10dp" >

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/toast_text"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:gravity="center_vertical"
    android:textColor="#FFF" />

</LinearLayout>
于 2013-03-01T10:15:58.240 回答