From 59bb3a049d88d51eadb5e88fba465be558ba564a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8A=B2=E9=B9=8F?= Date: Thu, 16 Sep 2021 15:49:55 +0800 Subject: [PATCH] Android: fix toast gravity above api level R --- .../java/pub/doric/plugin/ModalPlugin.java | 32 ++++++++++++------- .../src/main/res/drawable/shape_toast.xml | 6 ++++ .../src/main/res/layout/doric_modal_toast.xml | 18 +++++++++++ .../doric/src/main/res/values/styles.xml | 5 +++ 4 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 doric-android/doric/src/main/res/drawable/shape_toast.xml create mode 100644 doric-android/doric/src/main/res/layout/doric_modal_toast.xml diff --git a/doric-android/doric/src/main/java/pub/doric/plugin/ModalPlugin.java b/doric-android/doric/src/main/java/pub/doric/plugin/ModalPlugin.java index a41a1128..77f6de81 100644 --- a/doric-android/doric/src/main/java/pub/doric/plugin/ModalPlugin.java +++ b/doric-android/doric/src/main/java/pub/doric/plugin/ModalPlugin.java @@ -17,6 +17,7 @@ package pub.doric.plugin; import android.app.AlertDialog; import android.content.DialogInterface; +import android.os.Build; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; @@ -24,18 +25,18 @@ import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; -import pub.doric.DoricContext; -import pub.doric.R; -import pub.doric.extension.bridge.DoricPlugin; -import pub.doric.extension.bridge.DoricMethod; -import pub.doric.extension.bridge.DoricPromise; -import pub.doric.utils.DoricUtils; -import pub.doric.utils.ThreadMode; - import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSValue; import com.github.pengfeizhou.jscore.JavaValue; +import pub.doric.DoricContext; +import pub.doric.R; +import pub.doric.extension.bridge.DoricMethod; +import pub.doric.extension.bridge.DoricPlugin; +import pub.doric.extension.bridge.DoricPromise; +import pub.doric.utils.DoricUtils; +import pub.doric.utils.ThreadMode; + /** * @Description: Doric * @Author: pengfei.zhou @@ -57,9 +58,18 @@ public class ModalPlugin extends DoricJavaPlugin { if (gravityVal.isNumber()) { gravity = gravityVal.asNumber().toInt(); } - Toast toast = Toast.makeText(getDoricContext().getContext(), - msg, - Toast.LENGTH_SHORT); + Toast toast; + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) { + toast = Toast.makeText(getDoricContext().getContext(), msg, Toast.LENGTH_SHORT); + } else { + toast = new Toast(getDoricContext().getContext()); + View view = LayoutInflater.from(getDoricContext().getContext()).inflate(R.layout.doric_modal_toast, null, false); + TextView msgTV = view.findViewById(R.id.tv_msg); + msgTV.setText(msg); + toast.setView(view); + toast.setDuration(Toast.LENGTH_SHORT); + } + if ((gravity & Gravity.TOP) == Gravity.TOP) { toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, DoricUtils.dp2px(50)); } else if ((gravity & Gravity.BOTTOM) == Gravity.BOTTOM) { diff --git a/doric-android/doric/src/main/res/drawable/shape_toast.xml b/doric-android/doric/src/main/res/drawable/shape_toast.xml new file mode 100644 index 00000000..b5732ce1 --- /dev/null +++ b/doric-android/doric/src/main/res/drawable/shape_toast.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/doric-android/doric/src/main/res/layout/doric_modal_toast.xml b/doric-android/doric/src/main/res/layout/doric_modal_toast.xml new file mode 100644 index 00000000..f7a8e42b --- /dev/null +++ b/doric-android/doric/src/main/res/layout/doric_modal_toast.xml @@ -0,0 +1,18 @@ + + + + + \ No newline at end of file diff --git a/doric-android/doric/src/main/res/values/styles.xml b/doric-android/doric/src/main/res/values/styles.xml index 9ba3f2f0..12f77f29 100644 --- a/doric-android/doric/src/main/res/values/styles.xml +++ b/doric-android/doric/src/main/res/values/styles.xml @@ -10,6 +10,11 @@ +