[原]unity3d调用android webView

1、配置dialog xml文件:

<resources>
    <style name="dialogStyleWindow" parent="@android:style/Theme.Dialog">
         <item name="android:windowNoTitle">true</item>  
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:background">@android:color/transparent</item>
        <item name="android:windowBackground">@android:color/transparent</item>  
    </style>
</resources>

2、创建布局xml notice.xml

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

    <WebView
        android:id="@+id/webView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <ImageView
        android:id="@+id/close"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/btn_close" />
</RelativeLayout>

3、代码实现:

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

public class Notice {

    Activity mActivity;
    Dialog noticeDialog = null;
    WebView webView;
    ProgressDialog dialog;
    

//在主activity中调用 传this进来
    public  Notice(Activity activity) {
        mActivity = activity;
    }
    

//u3d 调用这个函数显示webview  线程问题 建议通过handler方式调用
    public  void show() {
        if(noticeDialog == null){
            noticeDialog = new Dialog(mActivity,R.style.dialogStyleWindow);
        }
        View contentView = mActivity.getLayoutInflater().inflate(R.layout.notice,null);

         ImageView closeBtn = (ImageView) contentView
                 .findViewById(R.id.close); //关闭当前webview
         closeBtn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                
                noticeDialog.dismiss();
                webView = null;
                noticeDialog = null;
            }
        });
         
         dialog = ProgressDialog.show(mActivity,null,"正在进入网页,请稍后…");  
         webView =(WebView)contentView.findViewById(R.id.webView1);
        webView.loadUrl("http://www.baidu.com");  
        webView.getSettings().setJavaScriptEnabled(true);  
        webView.setWebViewClient(new WebViewClient());
        
        noticeDialog.setContentView(contentView);
        noticeDialog.setCanceledOnTouchOutside(false);
        noticeDialog.show();
        

 //设置webview全屏
        WindowManager windowManager = mActivity.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = noticeDialog.getWindow().getAttributes();
        lp.width = (int)(display.getWidth()); //设置宽度
        lp.height = (int)(display.getHeight());
        noticeDialog.getWindow().setAttributes(lp);
    }
     private class WebViewClient extends android.webkit.WebViewClient {  
            @Override  
            public boolean shouldOverrideUrlLoading(WebView view, String url) {  
              view.loadUrl(url);  
              return true;  
            }  
            
            @Override
            public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
               
                    dialog.dismiss();
            }
          }  

}

posted @ 2014-08-22 15:24  U_探索  阅读(1945)  评论(0编辑  收藏  举报