Customize your own Toast,it can be used in every thread。
first: Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
second:Add the dependency
dependencies {
compile 'com.github.jiekesiji:CustomToast:v1.2.2'
}
//强制传入的context必须是ApplicationContext,不然程序报异常
CustomToast.makeToast(mContext, content)
.setTextColor(Color.WHITE)
.setTextSize(18)
.setPadding(32, 24, 32, 24)
.setTextImage(iconResId)
// 这两个方法有先后顺序
.setTextImageSize(80, 80)
.setTextImageLocation(CustomToast.LEFT)
.setImagePadding(50)
.setBackgroundColor(Color.parseColor("#" + bgColor))
.setBackgroundRadius(10)
.setGravity(Gravity.BOTTOM, 0, 100);
方法 | 功能 |
---|---|
setTextImageLocation(100,100) | 设置Toast位置,单位px,相对屏幕中心位置 |
setBackgroundColor(Color.parseColor("#88000000")) | 设置背景,参数1背景颜色,参数2圆角弧度 |
setPadding(20,20,20,20) | 设置内边距 |
setTextColor(Color.BLUE) | 设置字体颜色 |
setTextSize(32) | 设置字体大小 |
setTextImage(R.mipmap.ic_launcher) | 设置想要添加的图片 |
setDuration(Toast.LENGTH_LONG) | 显示Toast时长,精确到毫秒,能够完全自定义 |
showMToast(Toast.LENGTH_LONG) | 显示Toast |
clearSetting() | 清除之前设置 |
- 传入的上下文必须是ApplicationContext,不然回报异常
- 建议设置玩text相关属性,再设置image属性
- 先设置图片大小在设置图片位置,不然会报错。