A beautifully simple to use third-party libraries Toast

  • 'Ve been using Android native Toast, Toast personally feel this thing, no need to spend effort, saw Toasty know this stuff, immediately turns pink, really very nice.
  • project address

We all know that Android usage is native Toast

1 Toast.makeText(MainActivity.this,"Toast显示内容",Toast.LENGTH_SHORT).show();

The method of the Context, content, duration (LENGTH_SHORT is short, as long .LENGTH_LONG 2 seconds, 3.5 seconds may be used instead of a few milliseconds) configuration. The actual use of Toasty, but also consistent with the method uses native, which is very comfortable.

 

First, we need to add a warehouse in the root directory of the project build.gradle

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

 

Then, build.gradle in the app directory add dependencies

1  the Dependencies {
 2      ...
 3      // By the time of writing this blog for the latest version 1.4.2 
4      Implementation 'com.github.GrenderG: Toasty: 1.4.2'
 5 }

 

Completion of the above preparations, now ready for use

 

Error Toast

1 Toasty.error(MainActivity.this, "This is an error toast.", Toast.LENGTH_SHORT, true).show();

 

Toast Success

1 Toasty.success(MainActivity.this, "This is a success toast.", Toast.LENGTH_SHORT, true).show();

 

Tips Toast

1 Toasty.info(MainActivity.this, "This is an info toast.", Toast.LENGTH_SHORT, true).show();

 

Warning Toast

1 Toasty.warning(MainActivity.this, "This is a warning toast.", Toast.LENGTH_SHORT, true).show();

 

Primeval Toast

Of course, there are also modified native Toast, meet your different needs

1 Toasty.normal(MainActivity.this, "Normal toast.").show();

 

Native Toast with pictures

1 Toasty.normal (. MainActivity the this , "Normal Toast." , IconDrawable) .Show ();
 2  // IconDrawable that you need to put the picture, you can use getDrawable achieved. Toast software icon when pictures, for example, the usage is: 
3 Toasty.normal (MainActivity. The this , "Normal Toast.", GetDrawable (R.drawable.ic_launcher)) Show ();.

 

Custom Toast

Of course definitely not Custom Toast

1  // official description is: 
2 Toasty.custom (yourContext, "the I'm A Custom Toast" , yourIconDrawable, the tintColor, DURATION, withIcon, shouldTint) the .Show ();

However, in actual use, they need to complement their own steps

Picture still use getDrawable, and colors can be used directly Andrews set defined Color.BLUE, Color.RED, respectively, set the background color and text color, is used as follows:

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

However, in practice, of course, you need custom colors. This can be achieved in a similar getColor:

1. Set the color values ​​under colors.xml / res / valus directory

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

 2. Use getColor references color1 and 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();

 

Unified configuration

Toasty support unified configuration content, but I did not go to the current study, interested persons can go to the original source code and project view readme to learn to use.

 

Show results

 

related resources

Download Demo demo

Download Source

Guess you like

Origin www.cnblogs.com/bigcn/p/12172780.html