如何以固定的宽度和高度显示Webview

如何以固定的宽度和高度显示Webview,第1张

如何以固定的宽度高度显示Webview

您可以使用

LinearLayout
而不是
RelativeLayout
拆分屏幕。只需将高度设置为0并将重量设置为1
GLSurfaceView
并将
WebView
两者都设置。在您的情况下,代码将为:

LinearLayout layout = new LinearLayout(this); // Use LinearLayout instead of RelativeglView = new GLSurfaceView(this);glView.setRenderer(this);glView.setZOrderMediaOverlay(false);// height is 0, weight is 1LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1);layout.addView(glView, params);webView = new WebView(this);this.showWV(false); //handler message , i hide it in certain screen.webView.getSettings().setJavascriptEnabled(true);webView.getSettings().setLoadWithOverviewMode(true);webView.getSettings().setUseWideViewPort(true);// height is 0, weight is 1params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1);layout.addView(webView, params);webView.loadUrl("http://stackoverflow.com");setContentView(layout);

或者,您可以使用

RelativeLayout
编码的高度和宽度值:

RelativeLayout layout = new RelativeLayout(this); // Use LinearLayout instead of RelativeglView = new GLSurfaceView(this);glView.setRenderer(this);glView.setZOrderMediaOverlay(false);glView.setId(123); // set idRelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(640, 480);layout.addView(glView, params);webView = new WebView(this);this.showWV(false); //handler message , i hide it in certain screen.webView.getSettings().setJavascriptEnabled(true);webView.getSettings().setLoadWithOverviewMode(true);webView.getSettings().setUseWideViewPort(true);params = new RelativeLayout.LayoutParams(640, 480);params.addRule(RelativeLayout.BELOW, glView.getId()); // set WebView position is below GLSurfaceViewlayout.addView(webView, params);webView.loadUrl("http://stackoverflow.com");setContentView(layout);

UPD: 不带硬编码值,使用不可见视图:

RelativeLayout layout = new RelativeLayout(this); // Use LinearLayout instead of Relative// create a fake view with zero size and place it to center of RelativeLayoutView fakeView = new View(this); fakeView.setId(24736); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(0, 0);params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);layout.addView(fakeView, params);glView = new GLSurfaceView(this);glView.setRenderer(this);glView.setZOrderMediaOverlay(false);glView.setId(123); // set idparams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);params.addRule(RelativeLayout.ABOVE, fakeView.getId()); // set position is above fakeViewlayout.addView(glView, params);webView = new WebView(this);this.showWV(false); //handler message , i hide it in certain screen.webView.getSettings().setJavascriptEnabled(true);webView.getSettings().setLoadWithOverviewMode(true);webView.getSettings().setUseWideViewPort(true);params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);params.addRule(RelativeLayout.BELOW, fakeView.getId()); // set WebView position is below fakeViewlayout.addView(webView, params);webView.loadUrl("http://stackoverflow.com");setContentView(layout);


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5475926.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存