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

}


文章转载自 unity3d调用android webView,感谢 U_探索 提供好文章

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值