Skip to content

Commit a46bfff

Browse files
committed
兼容 Android 11 新特性
1 parent 4654e7b commit a46bfff

File tree

8 files changed

+84
-14
lines changed

8 files changed

+84
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#### 集成步骤
1616

1717
dependencies {
18-
implementation 'com.hjq:toast:8.5'
18+
implementation 'com.hjq:toast:8.6'
1919
}
2020

2121
#### 初始化 Toast

ToastUtils.apk

1.09 MB
Binary file not shown.

app/build.gradle

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
apply plugin: 'com.android.application'
22

33
android {
4-
compileSdkVersion 28
4+
compileSdkVersion 30
55

66
defaultConfig {
77
applicationId "com.hjq.toast.demo"
88
minSdkVersion 14
9-
targetSdkVersion 28
10-
versionCode 85
11-
versionName "8.5"
9+
targetSdkVersion 30
10+
versionCode 86
11+
versionName "8.6"
1212
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1313
}
1414
buildTypes {
@@ -22,7 +22,8 @@ android {
2222
dependencies {
2323
implementation fileTree(include: ['*.jar'], dir: 'libs')
2424
implementation project(':library')
25-
implementation 'com.android.support:appcompat-v7:28.0.0'
25+
implementation 'androidx.appcompat:appcompat:1.3.0-alpha01'
26+
implementation 'com.google.android.material:material:1.3.0-alpha01'
2627
// 标题栏:https://github.com/getActivity/TitleBar
2728
implementation 'com.hjq:titlebar:6.5'
2829
// 悬浮窗:https://github.com/getActivity/XToast

app/src/main/java/com/hjq/toast/demo/ToastActivity.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.hjq.toast.demo;
22

33
import android.os.Bundle;
4-
import android.os.Handler;
5-
import android.support.v4.app.NotificationManagerCompat;
6-
import android.support.v7.app.AppCompatActivity;
74
import android.view.Gravity;
85
import android.view.View;
96

7+
import androidx.appcompat.app.AppCompatActivity;
8+
import androidx.core.app.NotificationManagerCompat;
9+
1010
import com.hjq.toast.SupportToast;
1111
import com.hjq.toast.ToastUtils;
1212
import com.hjq.toast.style.ToastAliPayStyle;

library/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ android {
77
defaultConfig {
88
minSdkVersion 3
99
targetSdkVersion 26
10-
versionCode 85
11-
versionName "8.5"
10+
versionCode 86
11+
versionName "8.6"
1212
}
1313
}
1414

1515
publish {
1616
userOrg = 'getactivity'
1717
groupId = 'com.hjq'
1818
artifactId = 'toast'
19-
version = '8.5'
19+
version = '8.6'
2020
description = 'This is a very functional Toast'
2121
website = "https://github.com/getActivity/ToastUtils"
2222
}

library/src/main/java/com/hjq/toast/BaseToast.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,22 @@ public BaseToast(Application application) {
2424
@Override
2525
public void setView(View view) {
2626
super.setView(view);
27-
mMessageView = getMessageView(view);
27+
setMessageView(findMessageView(view));
2828
}
2929

3030
@Override
3131
public void setText(CharSequence s) {
3232
mMessageView.setText(s);
3333
}
3434

35+
void setMessageView(TextView textView) {
36+
mMessageView = textView;
37+
}
38+
3539
/**
3640
* 智能获取用于显示消息的 TextView
3741
*/
38-
private static TextView getMessageView(View view) {
42+
static TextView findMessageView(View view) {
3943
if (view instanceof TextView) {
4044
return (TextView) view;
4145
} else if (view.findViewById(android.R.id.message) instanceof TextView) {

library/src/main/java/com/hjq/toast/SupportToast.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.hjq.toast;
22

33
import android.app.Application;
4+
import android.view.View;
45

56
/**
67
* author : Android 轮子哥
@@ -13,6 +14,19 @@ public final class SupportToast extends BaseToast {
1314
/** 吐司弹窗显示辅助类 */
1415
private final ToastHelper mToastHelper;
1516

17+
/** Toast 的视图 */
18+
private View mView;
19+
/** Toast 的重心 */
20+
private int mGravity;
21+
/** 水平偏移 */
22+
private int mXOffset;
23+
/** 垂直偏移 */
24+
private int mYOffset;
25+
/** 水平间距百分比 */
26+
private float mHorizontalMargin;
27+
/** 垂直间距百分比 */
28+
private float mVerticalMargin;
29+
1630
public SupportToast(Application application) {
1731
super(application);
1832
mToastHelper = new ToastHelper(this, application);
@@ -29,4 +43,53 @@ public void cancel() {
2943
// 取消显示
3044
mToastHelper.cancel();
3145
}
46+
47+
@Override
48+
public void setView(View view) {
49+
mView = view;
50+
setMessageView(findMessageView(view));
51+
}
52+
53+
@Override
54+
public View getView() {
55+
return mView;
56+
}
57+
58+
@Override
59+
public void setGravity(int gravity, int xOffset, int yOffset) {
60+
mGravity = gravity;
61+
mXOffset = xOffset;
62+
mYOffset = yOffset;
63+
}
64+
65+
@Override
66+
public int getGravity() {
67+
return mGravity;
68+
}
69+
70+
@Override
71+
public int getXOffset() {
72+
return mXOffset;
73+
}
74+
75+
@Override
76+
public int getYOffset() {
77+
return mYOffset;
78+
}
79+
80+
@Override
81+
public void setMargin(float horizontalMargin, float verticalMargin) {
82+
mHorizontalMargin = horizontalMargin;
83+
mVerticalMargin = verticalMargin;
84+
}
85+
86+
@Override
87+
public float getHorizontalMargin() {
88+
return mHorizontalMargin;
89+
}
90+
91+
@Override
92+
public float getVerticalMargin() {
93+
return mVerticalMargin;
94+
}
3295
}

library/src/main/java/com/hjq/toast/ToastHelper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ void show() {
9595
params.gravity = mToast.getGravity();
9696
params.x = mToast.getXOffset();
9797
params.y = mToast.getYOffset();
98+
params.verticalMargin = mToast.getVerticalMargin();
99+
params.horizontalMargin = mToast.getHorizontalMargin();
98100

99101
try {
100102
Activity topActivity = mWindowHelper.getTopActivity();

0 commit comments

Comments
 (0)