Android: fix toast gravity above api level R

This commit is contained in:
王劲鹏 2021-09-16 15:49:55 +08:00 committed by osborn
parent 417052534a
commit 59bb3a049d
4 changed files with 50 additions and 11 deletions

View File

@ -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) {

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#E6EEEEEE" />
<corners android:radius="22dp" />
</shape>

View File

@ -0,0 +1,18 @@
<?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="match_parent"
android:background="@drawable/shape_toast"
android:orientation="vertical">
<TextView
android:id="@+id/tv_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginHorizontal="24dp"
android:layout_marginVertical="15dp"
android:layout_weight="1"
android:textAppearance="@style/Theme.Doric.Modal.Toast"
android:textColor="@color/primary_text_default_material_light" />
</LinearLayout>

View File

@ -10,6 +10,11 @@
<style name="Theme.Doric.Modal.Prompt" parent="Theme.Doric.Modal" />
<style name="Theme.Doric.Modal.Toast" parent="Theme.Doric.Modal">
<item name="fontFamily">sans-serif</item>
<item name="android:textSize">14sp</item>
</style>
<style name="Theme.Doric.Modal.Prompt.EditText" parent="Theme.AppCompat.Light" />
</resources>