2011-09-23 33 views
1

我正尝试将web视图加载到对话框中。我正在使用以下代码。如何在android中的diaoge中加载web视图

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

我想打开一个文本视图上点击的网页视图。当我点击文本视图对话框打开没有webview。任何人都可以帮助我解决这个问题。

在此先感谢。

回答

5

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

之后设置web视图像这样: web视图WV =(web视图)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(); 
     } 
    }); 
+0

嗨blou,感谢您的快速响应。我仍然遇到同样的问题。你认为网页浏览工作需要什么吗?我已经为清单中的互联网添加了许可。 – user519846

+0

我犯了错误的代码findViewByID应该是dialog.findViewById –

+0

使用Android Studio 1.5不能使用此方法,它表示'错误:Android-XML:将放在使用wrap_content大小的父元素可能会导致微妙的错误;使用match_parent' – Choletski

-1

我建议使用webview布局创建一个活动,并添加任何其他内容。

而当你在Android清单中注册时,使用这个tage。

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

为什么这样投下来?请在评论中注明或不要低估它! –

+0

我也推荐这个答案 –

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

,以显示呼叫的ShowDialog(1);

+0

谢谢安迪,谢谢你的回应。让我试试它。 – user519846

1

设置的WebView高度和宽度,它会工作。