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);