Toast在Android中的定义就是大家所熟悉的黑色半透明提示,我们已经对其了解,但是黑色半透明怎么可能满足我们呢。所以就出现了两个很好的Toast库
compile 'com.github.GrenderG:Toasty:1.2.5'
compile 'com.muddzdev:styleabletoast:1.0.9'
这个两个是在GitHub中Star很高的项目。
先说第一个Toasty,在项目中为我们提供了很多的种类,也支持自定义。
Drawable icon = getResources().getDrawable(R.drawable.ic_pets_white_48dp);
Toasty.normal(MainActivity.this, "普普通通", icon).show();
Toasty.normal(MainActivity.this, "普普通通").show();
Toasty.error(MainActivity.this, "失败.", Toast.LENGTH_SHORT, true).show();
Toasty.success(MainActivity.this, "成功!", Toast.LENGTH_SHORT, true).show();
Toasty.warning(MainActivity.this, "内有恶犬.", Toast.LENGTH_SHORT, true).show();
Toasty.info(MainActivity.this, "这里有一条信息给你.", Toast.LENGTH_SHORT, true).show();
Toasty.info(MainActivity.this, getFormattedMessage()).show();
private CharSequence getFormattedMessage() {
final String prefix = "看我 ";
final String highlight = "不打你";
final String suffix = " 好吧";
SpannableStringBuilder ssb = new SpannableStringBuilder(prefix).append(highlight).append(suffix);
int prefixLen = prefix.length();
ssb.setSpan(new StyleSpan(BOLD_ITALIC),
prefixLen, prefixLen + highlight.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return ssb;
}
Toasty.Config.getInstance()
.setTextColor(Color.GREEN)
.setToastTypeface(Typeface.createFromAsset(getAssets(), "PCap Terminal.otf"))
.apply();
Toasty.custom(MainActivity.this, "炸锅卖铁贼酷炫 It is OK", getResources().getDrawable(R.drawable.laptop512),
Color.BLACK, Toast.LENGTH_SHORT, true, true).show();
Toasty.Config.reset();//此配置仅这次使用
第二个StyleableToast,自定义更方便
StyleableToast.makeText(this, "关闭飞行模式", Toast.LENGTH_LONG, R.style.StyleableToast).show();
StyleableToas styleableToast = new StyleableToast
.Builder(this)
.text("软件有新的更新")
.textColor(Color.WHITE)
.icon(R.drawable.ic_file_download)
.backgroundColor(Color.parseColor("#23ad33"))
.build();
styleableToast = new StyleableToast
.Builder(this)
.duration(Toast.LENGTH_LONG)
.text("你的系统升级成功")
.textColor(Color.WHITE)
.typeface(Typeface.createFromAsset(getAssets(), "dosis.otf"))
.backgroundColor(Color.parseColor("#cc3784"))
.build();
styleableToast = new StyleableToast
.Builder(this)
.text("感谢您的捐赠!")
.textColor(Color.parseColor("#6063b2"))
.strokeWidth(2)
.duration(Toast.LENGTH_LONG)
.strokeColor(Color.parseColor("#989ad1"))
.backgroundColor(Color.WHITE)
.build();
styleableToast = new StyleableToast
.Builder(this)
.icon(R.drawable.ic_overheating)
.text("手机过热!")
.textBold()
.textColor(Color.parseColor("#FFDA44"))
.cornerRadius(5)
.build();
styleableToast = new StyleableToast
.Builder(this)
.duration(Toast.LENGTH_LONG)
.icon(R.drawable.ic_autorenew_black_24dp)
.spinIcon()
.text("下载信息")
.textColor(Color.WHITE)
.backgroundColor(Color.parseColor("#184c6d"))
.build();
欢迎大家互相交流,以上代码在
https://github.com/primerToforget/ToaseyTest
官方GitHub
https://github.com/GrenderG/Toasty
https://github.com/Muddz/StyleableToast