WebView slow on loading content

2019-07-24 19:32发布

问题:

I am doing a custom dialog plugin for PhoneGap in Android. The Notification plugin for PhoneGap cannot set text in HTML format in dialog boxes. So I decided to make my own using WebView. But I noticed that when the Alert.Dialog appears, the WebView is initially empty and then the content is loaded a split second later causing the dialog height to "jump". Even when I hardcode the content passed to loadData, the loading of content is still slow.

Is there anything I'm missing?

@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
    try {
        JSONObject jo = args.getJSONObject(0);
        showCustomDialog(jo.getString("title"), jo.getString("message"), jo.getString("buttonLabel")); 
        return new PluginResult(PluginResult.Status.OK);
    } catch (JSONException e) {
        return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
    }
}

private void showCustomDialog(final String dialogTitle, final String dialogMessage, final String dialogButtonLabel) {           

    Runnable runnable = new Runnable() {

        public void run() {                         
            WebView webView = new WebView(ctx);
            webView.loadData(dialogMessage, "text/html", "UTF-8");
        webView.setBackgroundColor(Color.TRANSPARENT);

        AlertDialog.Builder alert = new AlertDialog.Builder(ctx);           
        alert.setView(view)
            .setCancelable(false)
            .setIcon(R.drawable.icon)
            .setTitle(dialogTitle)
            .setPositiveButton(dialogButtonLabel,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.dismiss();
                        }
                    }
            )
            .show();
        }
    };
    this.ctx.runOnUiThread(runnable);
}