描述

其实默认的Toast也还挺可爱的,但是有时候我就想自己去定义它:

比如在上面显示

比如固定大小

巴拉巴拉。

实现

SuperToast

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
    /**
* 错误提示
*
* @param content
*/
public static void error(String content) {
show(content, R.drawable.shape_toast_error_background, Toast.LENGTH_SHORT);
}

/**
* 成功提示
*
* @param content
*/
public static void success(@StringRes int content) {
show(context.getString(content), R.drawable.shape_toast_success_background, Toast.LENGTH_SHORT);
}

public static void success(String content) {
show(content, R.drawable.shape_toast_success_background, Toast.LENGTH_SHORT);
}

public static void show(String content, @DrawableRes int background, int duration) {
show(content, background, duration, Gravity.FILL_HORIZONTAL | Gravity.TOP, 0, 130);
}

/**
* 显示
*
* @param content 显示内容
* @param background 背景
* @param duration 时长
* @param gravity 位置
* @param xOffset x轴偏移
* @param yOffset y轴偏移
*/
public static void show(String content, @DrawableRes int background, int duration, int gravity, int xOffset, int yOffset) {
Toast toast = new Toast(context);

//设置时长
toast.setDuration(duration);

//设置位置
toast.setGravity(gravity, xOffset, yOffset);

// 加载布局
View layout = inflater.inflate(R.layout.super_toast, null);
LinearLayout containerView = layout.findViewById(R.id.container);

//设置背景
containerView.setBackgroundResource(background);

TextView contentView = layout.findViewById(R.id.content);
contentView.setText(content);

//设置自定义布局
toast.setView(layout);

//显示
toast.show();
}
}

补充

R.drawable.shape_toast_background:默认Toast的黑色背景。

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--圆角-->
<corners android:radius="@dimen/radius_small" />

<!--填充颜色-->
<solid android:color="@color/super_toast_background" />
</shape>

R.drawable.shape_toast_error_background:错误提示的红色背景

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--圆角-->
<corners android:radius="5dp" />

<!--填充颜色-->
<solid android:color="@color/super_toast_error_background" />
</shape>

R.drawable.shape_toast_success_background:成功Toast的绿色背景。

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--圆角-->
<corners android:radius="5dp" />

<!--填充颜色-->
<solid android:color="@color/super_toast_success_background" />
</shape>

Toast自己的xml:R.layout.super_toast

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingHorizontal="20dp">

<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape_toast_background"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:paddingHorizontal="10dp"
android:paddingVertical="16dp">

<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="提示信息"
android:textColor="@color/white"
android:textSize="@dimen/s16" />
</LinearLayout>
</LinearLayout>

他这里实现圆弧是用自定义背景那个。

@drawable/shape_toast_background

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--圆角-->
<corners android:radius="@dimen/radius_small" />

<!--填充颜色-->
<solid android:color="@color/super_toast_background" />
</shape>

使用

APP唯一初始化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class AppContext extends Application {
private static AppContext instance;


@Override
public void onCreate() {
super.onCreate();
instance = this;
initMMKV();

//初始化Toast工具类
SuperToast.init(getApplicationContext());
}
}

随意使用

1
2
3
SuperToast.show("干什么?");
SuperToast.erro("干什么?");
SuperToast.success("干什么?");