见链接here。你找到你的解决方案。并尝试:
创建自定义 Toast 视图
如果简单的短信还不够,您可以为 Toast 通知创建自定义布局。要创建自定义布局,请在 XML 或应用程序代码中定义 View 布局,并将根 View 对象传递给 setView (View) 方法。
例如,您可以使用以下 XML(保存为 toast_layout.xml)为右侧屏幕截图中可见的 toast 创建布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:background="#DAAA"
>
<ImageView android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"
/>
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF"
/>
</LinearLayout>
请注意,LinearLayout 元素的 ID 是“toast_layout”。您必须使用此 ID 从 XML 中扩展布局,如下所示:
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout,
(ViewGroup) findViewById(R.id.toast_layout_root));
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello! This is a custom toast!");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
首先,使用 getLayoutInflater()(或 getSystemService())检索 LayoutInflater,然后使用 inflate(int, ViewGroup) 从 XML 中扩展布局。第一个参数是布局资源 ID,第二个参数是根视图。您可以使用此膨胀布局在布局中查找更多 View 对象,因此现在捕获并定义 ImageView 和 TextView 元素的内容。最后,使用 Toast(Context) 创建一个新的 Toast,并设置 Toast 的一些属性,例如重力和持续时间。然后调用 setView(View) 并将膨胀的布局传递给它。您现在可以通过调用 show() 以自定义布局显示 toast。
注意:除非您打算使用 setView(View) 定义布局,否则不要对 Toast 使用公共构造函数。如果您没有要使用的自定义布局,则必须使用 makeText(Context, int, int) 来创建 Toast。