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

抱歉,我一直在搜索此论坛,但没有确切答案。所以我问自己的。

我有一堂课,用目标纵向屏幕640x960显示glView和webview两种布局。我计划拆分布局。因此我的背景看起来是在(0,0,640,480)或纵向屏幕的一半处绘制的。,我想在(0,481,640,479)处绘制我的Web视图,该视图占据了从中间到底部的另一半。但是,我失败了,无法弄清楚如何实现我想要的布局。如何设置webView的大小和位置?

PS:如果我将布局设置为与底部对齐,则Web视图的确处于中间到底部的位置,但是我需要指定确切的位置..最坏的..当它加载url时,它将使用整个屏幕,覆盖我所有的第一个布局,我迷路了。因此如何使WebView将加载的URL显示到WebView中指定的宽度和高度。

谢谢..对不起,我的英语不好。

无论如何,这是我的代码(我以编程方式创建了所有代码,没有使用任何xml布局)

    glView = new GLSurfaceView(this);

glView.setRenderer(this);

glView.setZOrderMediaOverlay(false);

layout = new RelativeLayout(this);

layout.addView(glView);

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

webView.loadUrl("http://stackoverflow.com");

RelativeLayout.LayoutParams params =

new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,

RelativeLayout.LayoutParams.WRAP_CONTENT);

params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

layout.addView(webView, params);

setContentView(layout);

回答:

您可以使用LinearLayout而不是RelativeLayout拆分屏幕。只需将高度设置为0并将重量设置为1

GLSurfaceView并将WebView两者都设置。在您的情况下,代码将为:

LinearLayout layout = new LinearLayout(this); // Use LinearLayout instead of Relative

glView = new GLSurfaceView(this);

glView.setRenderer(this);

glView.setZOrderMediaOverlay(false);

// height is 0, weight is 1

LinearLayout.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 1

params = 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 Relative

glView = new GLSurfaceView(this);

glView.setRenderer(this);

glView.setZOrderMediaOverlay(false);

glView.setId(123); // set id

RelativeLayout.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 GLSurfaceView

layout.addView(webView, params);

webView.loadUrl("http://stackoverflow.com");

setContentView(layout);

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

RelativeLayout layout = new RelativeLayout(this); // Use LinearLayout instead of Relative

// create a fake view with zero size and place it to center of RelativeLayout

View 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 id

params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

params.addRule(RelativeLayout.ABOVE, fakeView.getId()); // set position is above fakeView

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(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

params.addRule(RelativeLayout.BELOW, fakeView.getId()); // set WebView position is below fakeView

layout.addView(webView, params);

webView.loadUrl("http://stackoverflow.com");

setContentView(layout);

以上是 如何以固定的宽度和高度显示Webview 的全部内容, 来源链接: utcz.com/qa/425264.html

回到顶部