简体   繁体   English

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

[英]How to Display Webview in fixed width and height

Sorry, I've been searching this forum and got no exact answer. 抱歉,我一直在搜索此论坛,但没有确切答案。 so I ask my own. 所以我问我自己。

I have a class that display two layouts , glView and webview , with target portrait screen 640x960 . 我有一堂课,用目标纵向屏幕640x960显示glView和webview两种布局。 i plan to split the layout. 我计划拆分布局。 so it would look that my background drawn at (0,0,640,480) or half of the portrait screen. 因此我的背景看起来是(0,0,640,480)或纵向屏幕的一半。 , and i want my webview drawn at (0,481,640,479) which is occupy the other half from the middle to the bottom. ,我想在(0,481,640,479)绘制我的Web视图,该视图占据了从中间到底部的另一半。 however, i failed and couldn't figure out how to achieve the layout i want. 但是,我失败了,无法弄清楚如何实现我想要的布局。 How to set the webView size & Position ? 如何设置webView的大小和位置?

PS: if i set the layout to align bottom then the webview is indeed in the middle to bottom position however i need to specify the exact position.. and the worst.. when it loads the url , it will use the whole screen, covering all my first layout and i am lost. PS:如果我将布局设置为与底部对齐,则Web视图的确处于中间到底部的位置,但是我需要指定确切的位置..最坏的..当它加载url时,它将使用整个屏幕,覆盖我所有的第一个布局,我迷路了。 so how to make the webview display the loaded url into the webview specified width and height. 因此如何使WebView将加载的URL显示到WebView中指定的宽度和高度。

Thank you.. sorry for my bad english. 谢谢..对不起,我的英语不好。

anyway, this is my code (i created all programatically, i didn't use any xml layout) 无论如何,这是我的代码(我以编程方式创建了所有代码,没有使用任何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);

You can use LinearLayout instead of RelativeLayout to split screen. 您可以使用LinearLayout而不是RelativeLayout来拆分屏幕。 Just set height = 0 and weight = 1 of GLSurfaceView and WebView both. 只需将GLSurfaceViewWebView高度设置为0并将重量设置为1。 In your case the code will be: 在您的情况下,代码将为:

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

Or you can use RelativeLayout with hardcoded height and width values: 或者,您可以将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);

UPD: Without hardcoded values, using invisible view: UPD:不带硬编码值,使用不可见视图:

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

You can do this using RelativeLayout.LayoutParams class: 您可以使用RelativeLayout.LayoutParams类来执行此操作:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(640, 480);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
webView.setLayoutParams(params);

then add the webview to the layout. 然后将webview添加到布局中。 You can locate the other view at the bottom of the screen with a new LayoutParams instance and addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); 您可以使用新的LayoutParams实例和addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);在屏幕底部找到另一个视图addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM