自定义简单的Toast,可自行修改文字、图片、样式

/**
     * 
     * @param context
     * @param text 直接传字符串
     * @param resId 图片ID
     */
    public static void showTwo(Context context, String text,int resId) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View layout = inflater.inflate(R.layout.toast_layout_two, null);
        toast_image = (ImageView) layout.findViewById(R.id.toast_image);
      //图片可以自己从外面传递进来换;
        toast_image.setBackgroundResource(resId);
        textV = (TextView) layout.findViewById(R.id.toast_text);
        textV.setText(text);
        if (mToast == null) {
        mToast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
        mToast.setGravity(Gravity.CENTER, 0, 0);
        mToast.setView(layout);
        } else {
        mToast.cancel();//关闭吐司显示
        mToast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
        //设置Toast位置的,如若不设置将会是系统默认Toast位置
        mToast.setGravity(Gravity.CENTER, 0, 0);
        mToast.setView(layout);
        }
        mToast.show();
    }
    /**
     * 传字段ID
     * @param context
     * @param text 文字ID
     * @param resId 图片ID
     */
    public static void showTwo(Context context, int text,int resId) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View layout = inflater.inflate(R.layout.toast_layout_two, null);
        toast_image = (ImageView) layout.findViewById(R.id.toast_image);
        //图片可以自己从外面传递进来换;
          toast_image.setBackgroundResource(resId);
        TextView textV = (TextView) layout.findViewById(R.id.toast_text);
        textV.setText(text);
        if (mToast == null) {
        mToast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
        mToast.setGravity(Gravity.CENTER, 0, 0);
        mToast.setView(layout);
        } else {
        mToast.cancel();//关闭吐司显示
        mToast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
        //设置Toast位置的
        mToast.setGravity(Gravity.CENTER, 0, 0);
        mToast.setView(layout);
        }
        mToast.show();

    }



源码下载地址:(初次发文,多多关注)

http://download.csdn.net/detail/qq_31850557/9532987

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值