WebView未在自定义对话框布局中加载

时间:2017-03-24 05:17:39

标签: android webview android-alertdialog

我正在尝试将网页上的pdf文档加载到 url (我在浏览器中访问并运行良好),当我在CustomView中加载 url AlertDialog我的WebView没有显示任何内容。

alert_dialog_webview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="10dp"
    android:paddingStart="10dp"
    android:paddingRight="10dp"
    android:paddingEnd="10dp"
    >
    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/web_view"
        ></WebView>

</RelativeLayout>

   private void showDoc(String fileName) {

        final View view = getActivity().getLayoutInflater().inflate(R.layout.alert_dialog_webview, null);

        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setCancelable(false);

        final WebView webview  = (WebView) view.findViewById(R.id.web_view);
        String url = BASE_URL+fileName;
        //webview.loadUrl(url);
        webview.setWebViewClient(new WebViewClient() {
            @SuppressWarnings("deprecation")
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @TargetApi(Build.VERSION_CODES.N)
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                webview.loadUrl(request.getUrl().toString());
                return true;
            }
        });




        String negativeText = getString(R.string.cancel);
        builder.setNegativeButton(negativeText,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                       dialog.dismiss();
                    }
                });

        final AlertDialog dialog = builder.create();
        dialog.setView(view);
 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialogInterface) {
                dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(ContextCompat.getColor(getContext(),R.color.colorAccentDark));

            }
        });
        // display dialog
        dialog.show();
    }

3 个答案:

答案 0 :(得分:1)

我对WebView内的AlertDialog有同样的问题,但我注意到WebView正在加载网址,但其高度非常小,因此很难知道是否是否加载页面。

我发现的技巧是你需要向WebView添加weight属性并在match_parent中使用height

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <WebView
        android:id="@+id/webview_terms"
        android:layout_width="match_parent"
        android:layout_height="match_parent" <!-- use match_parent -->
        android:layout_weight="1" <!-- add this line -->
        tools:ignore="InefficientWeight" />

    <Button
        android:id="@+id/btn_accept_terms"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:background="@drawable/background_button_selector"
        android:text="@string/accept_button"
        android:textColor="@android:color/white" />

</LinearLayout>

答案 1 :(得分:0)

Android Webview不支持PDF,虽然Chrome应用支持它。

但是,如果您想在应用中打开PDF,可以使用MuPDF Library

如果您仍想在Webview中打开PDF,可以使用google drive的网址打开pdf文件。

但是,就像一个网页打开上面带有谷歌工具栏的pdf一样。 查看此答案以实现此目的: - https://stackoverflow.com/a/42975739/3488710

答案 2 :(得分:0)

试试这个:)

private void showDoc(String fileName) {

            final View view = getActivity().getLayoutInflater().inflate(R.layout.alert_dialog_webview, null);

            AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
            builder.setCancelable(false);

            final WebView webview  = (WebView) view.findViewById(R.id.web_view);
            String url = "https://docs.google.com/viewer?url="+BASE_URL+fileName;
            webview.getSettings().setAllowFileAccess(true);
            webview.getSettings().setPluginState(WebSettings.PluginState.ON);
            webview.getSettings().setBuiltInZoomControls(true);
            webview.getSettings().setJavaScriptEnabled(true);
            webview.setWebChromeClient(new WebChromeClient());
            webview.loadUrl(url);

            String negativeText = getString(R.string.cancel);
            builder.setNegativeButton(negativeText,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                           dialog.dismiss();
                        }
                    });

            final AlertDialog dialog = builder.create();
            dialog.setView(view);
            dialog.setOnShowListener(new DialogInterface.OnShowListener() {
                @Override
                public void onShow(DialogInterface dialogInterface) {
                    dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(ContextCompat.getColor(getContext(),R.color.colorAccentDark));

                }
            });
            // display dialog
            dialog.show();
        }