当前位置: 移动技术网 > IT编程>移动开发>Android > 一款精美的Toast第三方库的简单使用

一款精美的Toast第三方库的简单使用

2020年01月10日  | 移动技术网IT编程  | 我要评论

环保袋制作,教学法有哪些,www.0818.cm

  • 以前一直用的安卓原生toast,个人感觉toast这东西,没必要花功夫,知道看到了toasty这东西,立刻被圈粉了,真的非常好看。
  • 项目地址

我们都知道,安卓原生toast的用法是

1 toast.maketext(mainactivity.this,"toast显示内容",toast.length_short).show();

方法内由context、内容、持续时间(length_short为短时间,2秒。length_long为长时间,3.5秒。也可以用毫秒数代替)构成。而toasty的实际使用,也跟原生的使用方法基本一致,这就非常舒服了。

 

首先,我们需要在工程根目录的build.gradle添加仓库

1 allprojects {
2     repositories {
3         ...
4         maven { url "https://jitpack.io" }
5     }
6 }

 

然后,在app目录下的build.gradle添加依赖

1 dependencies {
2     ...
3     //截止到写这篇博客时最新版为1.4.2
4     implementation 'com.github.grenderg:toasty:1.4.2'
5 }

 

完成了以上的准备工作,现在就可以使用了

 

错误toast

1 toasty.error(mainactivity.this, "this is an error toast.", toast.length_short, true).show();

 

成功toast

1 toasty.success(mainactivity.this, "this is a success toast.", toast.length_short, true).show();

 

提示toast

1 toasty.info(mainactivity.this, "this is an info toast.", toast.length_short, true).show();

 

警告toast

1 toasty.warning(mainactivity.this, "this is a warning toast.", toast.length_short, true).show();

 

原生toast

当然,也有修改后的原生toast,满足你的不同需求

1 toasty.normal(mainactivity.this, "normal toast.").show();

带图片的原生toast

1 toasty.normal(mainactivity.this, "normal toast.", icondrawable).show();
2 //icondrawable是你需要放入的图片,可以用getdrawable实现。以软件图标当toast图片为例,则用法为:
3 toasty.normal(mainactivity.this, "normal toast.", getdrawable(r.drawable.ic_launcher)).show();

 

自定义toast

当然肯定少不了自定义toast

1 //官方的介绍是:
2 toasty.custom(yourcontext, "i'm a custom toast", youricondrawable, tintcolor, duration, withicon, shouldtint).show();

但在实际使用时,则需要自己补全步骤

图片仍然用getdrawable,而颜色可以直接使用安卓定义的color.blue、color.red等分别设置背景颜色和文本颜色,用法如下:

1 toasty.custom(mainactivity.this, "i'm a custom toast", getdrawable(r.drawable.ic_launcher),color.blue,color.red, toast.length_short, true,true).show();

然而,实际中更需要的当然是自定义颜色。这可以用类似的getcolor实现:

1.在/res/valus目录下的colors.xml中设置颜色值

1 <resources>
2     <color name="color1">#bbdefb</color>
3     <color name="color2">#ffffff</color>
4 </resources>

 2.使用getcolor引用color1和color2

1 toasty.custom(mainactivity.this, "i'm a custom toast", getdrawable(r.drawable.ic_launcher),getcolor(r.color.color1),getcolor(r.color.color2), toast.length_short, true,true).show();

 

统一配置

toasty支持统一配置内容,但是我目前并没有去研究,感兴趣的人可以去原项目中查看readme和源码来学习使用。

 

效果展示

 

相关资源

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网