如何添加添加到自定义Toast的自定义图像...
答案 0 :(得分:1)
只需在您的toast_layout中添加一个ImageView,然后:
ImageView iv= (ImageView) layout.findViewById(R.id.image);
iv.setBackgroundResource(R.drawable.img1);//Or wathever you want!
这样可行。
答案 1 :(得分: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号线上做什么?
答案 2 :(得分:-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>