安卓 轮子哥 吐司框架学习
知止自律打卡-微信小程序APP
一款微信自律打卡小程序,支持自定义打卡项目、习惯记录、项目日记、专注计时与数据统计。自律打卡、学习打卡、读书打卡、健身打卡、运动打卡、减肥打卡、戒色打卡、养生打卡、戒烟、戒酒、戒熬夜等多种场景。万能打卡模板自由创建。可自定义每日打卡目标,记录习惯连续天数,可视化追踪成长进度。
了解产品详情 →
微信搜索「知止自律打卡」
1 介绍
导包
//吐司框架
implementation 'com.hjq:toast:8.8'
或
// https://mvnrepository.com/artifact/com.hjq/toast
implementation group: 'com.hjq', name: 'toast', version: '8.2'
这里导入8.8 ,后面的部分代码不能使用,原作者使用的是9.6版本
2 初始化框架
ToastApplication.java
package com.example.toastutils;
import android.app.Application;
import com.hjq.toast.ToastUtils;
public final class ToastApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// 初始化 Toast 框架
ToastUtils.init(this);
}
}
AndroidManifest.xml
<application
android:name=".ToastApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ToastUtils">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
3 普通Toast
ToastUtils.show("我是普通的 Toast");

4 子线程中弹出的吐司
new Thread(new Runnable() {
@Override
public void run() {
ToastUtils.show("我是子线程中弹出的吐司");
}
}).start();
5 动态切换白色吐司样式
ToastUtils.setStyle(new WhiteToastStyle());
ToastUtils.show("动态切换白色吐司样式成功");

6 动态切换黑色吐司样式
ToastUtils.setStyle(new BlackToastStyle());
ToastUtils.show("动态切换黑色吐司样式成功");

7 自定义 Toast 布局
res->layout:toast_custom_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/shape_gradient"
android:gravity="center"
android:orientation="horizontal"
android:padding="10dp">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@android:id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textColor="#FFFFFFFF"
android:textSize="16sp" />
</LinearLayout>
res->drawable:shape_gradient.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="180"
android:endColor="#387cfd"
android:startColor="#26c4fd"
android:type="linear" />
<corners android:radius="10dp" />
</shape>
ToastUtils.setView(R.layout.toast_custom_view);
ToastUtils.setGravity(Gravity.CENTER);
ToastUtils.show("自定义 Toast 布局");

8 后台显示Toast
if (XXPermissions.isGranted(this, Permission.NOTIFICATION_SERVICE)) {
Snackbar.make(getWindow().getDecorView(), "正在准备跳转到手机桌面,请注意有极少数机型无法在后台显示 Toast", Snackbar.LENGTH_SHORT).show();
v.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
}
}, 2000);
v.postDelayed(new Runnable() {
@Override
public void run() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
ToastUtils.show("我是在后台显示的 Toast(Android 11 及以上在后台显示 Toast 只能使用系统样式)");
} else {
ToastUtils.show("我是在后台显示的 Toast");
}
}
}, 3000);
} else {
ToastUtils.show("在后台显示 Toast 需要先获取通知栏权限");
v.postDelayed(new Runnable() {
@Override
public void run() {
XXPermissions.startPermissionActivity(ToastActivity.this, Permission.NOTIFICATION_SERVICE);
}
}, 2000);
}
9 搭配XToast悬浮窗框架
new XToast<>(this)
.setDuration(1000)
.setContentView(ToastUtils.getStyle().createView(getApplication()))
.setAnimStyle(android.R.style.Animation_Translucent)
.setText(android.R.id.message, "就问你溜不溜")
.setGravity(Gravity.BOTTOM)
.setYOffset((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics()))
.show();

参考地址:
https://github.com/getActivity/ToastUtils
标签: