Toast在Android中的定義就是大家所熟悉的黑色半透明提示醉拓,我們已經(jīng)對(duì)其了解金麸,但是黑色半透明怎么可能滿足我們呢。所以就出現(xiàn)了兩個(gè)很好的Toast庫
compile 'com.github.GrenderG:Toasty:1.2.5'
compile 'com.muddzdev:styleabletoast:1.0.9'
這個(gè)兩個(gè)是在GitHub中Star很高的項(xiàng)目幸海。
先說第一個(gè)Toasty,在項(xiàng)目中為我們提供了很多的種類慨丐,也支持自定義坡脐。
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, "內(nèi)有惡犬.", 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();//此配置僅這次使用
第二個(gè)StyleableToast,自定義更方便
StyleableToast.makeText(this, "關(guān)閉飛行模式", 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("你的系統(tǒng)升級(jí)成功")
.textColor(Color.WHITE)
.typeface(Typeface.createFromAsset(getAssets(), "dosis.otf"))
.backgroundColor(Color.parseColor("#cc3784"))
.build();
styleableToast = new StyleableToast
.Builder(this)
.text("感謝您的捐贈(zèng)!")
.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("手機(jī)過熱!")
.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