Android基础:自定义带图片的Toast

由于Android系统的默认Toast比较单调,而且不同手机型号Toast的显示也大一样。如下图所示,有些Toast需要能够显示图片,还要有一堆的透明度,而且显示位置也有要求,所以,为了满足项目的需求,我们需要用到自定义的Toast。



一、Toast布局文件

自定义Toast首先我们需要给Toast指定一个布局文件toast_email.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    <LinearLayout
        android:id="@+id/toast_emailOne_root"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/common_setting_backgroud_shape03"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingBottom="4dp"
        android:paddingLeft="6dp"
        android:paddingRight="6dp"
        android:paddingTop="4dp" >
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/prompt_report" />
        <TextView
            android:id="@+id/textToast"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="邮件已经,发送请注意查收!"
            android:textColor="@color/color14"
            android:textSize="@dimen/text_size4" />
    </LinearLayout>
</LinearLayout>
大概长这个样子


背景圆角与透明度的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <solid android:color="#77000000" />
    <corners
        android:bottomLeftRadius="20dp"
        android:bottomRightRadius="20dp"
        android:topLeftRadius="20dp"
        android:topRightRadius="20dp" />
</shape>

二、自定义Toast工具类

public class ToastEmail {
	public static ToastEmail mToastEmail;
	private Toast toast;

	private ToastEmail() {
	}

	public static ToastEmail getToastEmail() {
		if (mToastEmail == null) {
			mToastEmail = new ToastEmail();
		}
		return mToastEmail;
	}

	/**
	 * 显示
	 */
	public void ToastShow(Context context, ViewGroup root, String str) {
		View view = LayoutInflater.from(context).inflate(R.layout.toast_email, root);
		TextView text = (TextView) view.findViewById(R.id.textToast);
		text.setText(str); // 设置显示文字
		toast = new Toast(context);
		toast.setGravity(Gravity.CENTER, 0, 0); // Toast显示的位置
		toast.setDuration(2000); // Toast显示的时间
		toast.setView(view);
		toast.show();
	}

	public void ToastCancel() {
		if (toast != null) {
			toast.cancel();
		}
	}
}

三、自定义Toast的使用

使用的时候很简单,一句代码就能够搞定:
ToastEmail.getToastEmail().ToastShow(context, null, "我是Toast要显示的文字");
至此,简单带图片的Toast就介绍完了。

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值