//有时候为了实现某些效果,我们需要增加webview来实现某些功能,比如固定的footer部分,为了保证中间部分的效果和性能,这部分可以采用独立的webview。
//下面的代码演示了如何在Phonegap项目中增加自己的webview控件。
public class Main extends DroidGap
{
private static final String TAG = "Main";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
WebView webView2 = new WebView(this);
SetupWebView(webView2);
super.loadUrl("file:///android_asset/www/t3.html",1000);
webView2.loadUrl("file:///android_asset/www/t2.html");
}
/**
* Add a webview to the linearLayout The webView2 hold menubar.html
*/
private void SetupWebView(WebView webView2)
{
// 布局方向为垂直方向
this.root.setOrientation(LinearLayout.VERTICAL);
CordovaWebView webView = new CordovaWebView(this);
CordovaWebViewClient webViewClient;
if (android.os.Build.VERSION.SDK_INT < 11 /*
* android.os.Build.VERSION_CODES
* .HONEYCOMB
*/)
{
webViewClient = new CordovaWebViewClient(this, webView);
}
else
{
webViewClient = new IceCreamCordovaWebViewClient(this, webView);
}
// Set up web container
this.appView = webView;
this.appView.setId(100);
CordovaChromeClient webChromeClient = new CordovaChromeClient(this,
webView);
this.appView.setWebViewClient(webViewClient);
this.appView.setWebChromeClient(webChromeClient);
webViewClient.setWebView(this.appView);
webChromeClient.setWebView(this.appView);
// 第二个WebView占用屏幕的85%的高度,居于上方
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 0, 0.85f);
this.appView.setLayoutParams(params);
// Add web view but make it invisible while loading URL
this.appView.setVisibility(View.INVISIBLE);
this.root.addView(this.appView);
// 第二个WebView占用屏幕的15%的高度,居于下方
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 0, 0.15f);
webView2.setLayoutParams(params2);
this.root.addView(webView2);
setContentView(this.root);
// Clear cancel flag
this.cancelLoadUrl = false;
}
}
Android:WebApp同时使用WebView和Phonegap DroidGap
最新推荐文章于 2021-09-15 11:08:21 发布