如何在android中的dialoge中加载web视图

发布于 2024-12-05 21:04:00 字数 832 浏览 0 评论 0原文

我正在尝试将网络视图加载到对话框中。我正在使用以下代码。

    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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

养猫人 2024-12-12 21:04:00

使用包含 Webview 的 XML 布局。
然后调用dialog.setContentView(R.layout.web_dialog)

然后像这样设置webview:
WebView wv = (WebView) findViewById(R.id.webview);
“R.id.webview”是 XML 布局文件中的 ID。

对话框布局示例:

<?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>

您的代码已修改:

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();
        }
    });

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();
        }
    });
熊抱啵儿 2024-12-12 21:04:00
@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;
        } 

显示调用 showDialog(1);

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

甜宝宝 2024-12-12 21:04:00

设置webview的高度和宽度就可以了。

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

挖鼻大婶 2024-12-12 21:04:00

我建议使用 webview 布局创建一个活动以及您想要添加到其中的任何其他内容。

当您在 android 清单中注册它时,请使用此标记。

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

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">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文