自定义Android Toast

这里写图片描述

URL:http://blog.csdn.net/tounaobun/article/details/8866845

URL:http://stackoverflow.com/questions/11288475/custom-toast-in-android-a-simple-example


代码:

public void myToast(String message){
        Toast toast = Toast.makeText(this, message, Toast.LENGTH_LONG);
        View toastView = toast.getView(); //This'll return the default View of the Toast.

        /* And now you can get the TextView of the default View of the Toast. */
        TextView toastMessage = (TextView) toastView.findViewById(android.R.id.message);
        toastMessage.setTextSize(15);
        toastMessage.setTextColor(Color.RED);
        toastMessage.setCompoundDrawablesWithIntrinsicBounds(R.drawable.pig, 0, 0, 0);
        toastMessage.setGravity(Gravity.CENTER);
        toastMessage.setCompoundDrawablePadding(16);
        toastView.setBackgroundResource(R.drawable.corner);
        toast.show();
    }

调用如下:
myToast(“请输入[0,100)之间的整数”);

设置toast边角弧度:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="#f1e062" />
    <corners android:radius="10dp" />
    <padding
        android:bottom="5dp"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp" />
</shape>

这里写图片描述

URL:https://developer.android.com/guide/topics/ui/notifiers/toasts.html

public void makeCustomToast(String message){
        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.activity_toast,(ViewGroup) findViewById(R.id.custom_toast_container));

        TextView text = (TextView) layout.findViewById(R.id.toast_text);
        text.setText(message);

        ImageView image = (ImageView) layout.findViewById(R.id.toast_pic);
        image.setImageResource(R.drawable.pig);

        Toast toast = new Toast(getApplicationContext());
        toast.setGravity(Gravity.BOTTOM, 50, 50);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout);
        toast.show();
    }

调用:
this.makeCustomToast(“恭喜,猜对了”);


这里写图片描述

public class MyToast {
    public void show(Context context,String massage, int show_length) {
        //使用布局加载器,将编写的toast_layout布局加载进来
        View view = LayoutInflater.from(context).inflate(R.layout.activity_toast, null);
        //获取ImageView
        ImageView image = (ImageView) view.findViewById(R.id.toast_pic);
        //设置图片
        image.setImageResource(R.drawable.pig);
        //获取TextView
        TextView title = (TextView) view.findViewById(R.id.toast_text);
        //设置显示的内容
        title.setText(massage);
        Toast toast = new Toast(context);
        //设置Toast要显示的位置,水平居中并在底部,X轴偏移0个单位,Y轴偏移70个单位,
        toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, 70);
        //设置显示时间
        toast.setDuration(show_length);

        toast.setView(view);
        toast.show();
    }
}

调用:
MyToast toast = new MyToast();
toast.show(this,”对不起,您输入的数字太小,和预期不符,请重新输入”,Toast.LENGTH_SHORT);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值