How to load web view in dialoge in android

空扰寡人 提交于 2019-12-18 18:23:07

问题


I am trying to load web view in to dialog. I am usuing the following code.

    final TextView seeMonthlyBill = (TextView) parentLayout
            .findViewById(R.id.my_ac_my_service_timewarnercable_link);
    seeMonthlyBill.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Dialog dialog = new Dialog(getActivity());
            WebView wb = new WebView(getActivity());
            wb.getSettings().setJavaScriptEnabled(true);
            wb.loadUrl("http://www.google.com");
            wb.setWebViewClient(new HelloWebViewClient());
            dialog.setCancelable(true);
            dialog.setContentView(wb);
            dialog.setTitle("WebView");
            dialog.show();
        }
    });

I want to open web view on the click on a text view. When i am clicking on the text view dialog is opening without the webview. Can any please help me to solve this issue.

Thanks in advance.


回答1:


Use a XML layout containing a Webview. Then call dialog.setContentView(R.layout.web_dialog)

Set up the webview afterwards like so: WebView wv = (WebView) findViewById(R.id.webview); "R.id.webview" being the ID in the XML Layout file.

Dialog Layout example:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
>
    <WebView
        android:id="@+id/webview"
        android:scrollbars="vertical"
        android:scrollbarAlwaysDrawVerticalTrack="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />
</LinearLayout>

Your code modified:

final TextView seeMonthlyBill = (TextView) parentLayout
            .findViewById(R.id.my_ac_my_service_timewarnercable_link);
    seeMonthlyBill.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Dialog dialog = new Dialog(getActivity());
            dialog.setContentView(R.layout.web_dialog)
            WebView wb = (WebView) dialog.findViewById(R.id.webview);
            wb.getSettings().setJavaScriptEnabled(true);
            wb.loadUrl("http://www.google.com");
            wb.setWebViewClient(new HelloWebViewClient());
            dialog.setCancelable(true);
            dialog.setTitle("WebView");
            dialog.show();
        }
    });



回答2:


@Override

        protected Dialog onCreateDialog(int id) {

            // TODO Auto-generated method stub
                Dialog dialog = new Dialog(yourActivity.this);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                setDialogView(dialog,id);
                return dialog;
            return super.onCreateDialog(id);

        }


    private void setDialogView(final Dialog dialog,final int id) {

        // TODO Auto-generated method stub

        dialog.setContentView(R.layout.layoutContainingWebview);
        final WebView mWebView = (WebView)dialog.findViewById(R.id.WebviewId);
        mWebView.getSettings().setBuiltInZoomControls(true);
        mWebView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
        mWebView.setBackgroundColor(Color.TRANSPARENT);
        mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_INSET);
        mWebView.loadUrl("url");
        mWebView.setWebViewClient(new MyWebViewClient());
        dialog.setTitle("Feedback");  // for Feedback           
    }


    private class MyWebViewClient extends WebViewClient { 

        @Override 
        //show the web page in webview but not in web browser
        public boolean shouldOverrideUrlLoading(WebView view, String url) { 
            view.loadUrl (url); 
            return true;
        } 

TO show call showDialog(1);




回答3:


Set webview's height and width and it will work.




回答4:


What i would recommend is creating an activity with a webview layout and anything else you want to add to it.

And when you register it in the android manifest use this tage.

<activity android:theme="@android:style/Theme.Dialog">


来源:https://stackoverflow.com/questions/7527568/how-to-load-web-view-in-dialoge-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!