如何以固定的宽度和高度显示Webview [英] How to Display Webview in fixed width and height

查看:67
本文介绍了如何以固定的宽度和高度显示Webview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我一直在搜索该论坛,但没有确切答案.所以我问我自己.

Sorry, I've been searching this forum and got no exact answer. so I ask my own.

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

I have a class that display two layouts , glView and webview , with target portrait screen 640x960 . 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. , and i want my webview drawn at (0,481,640,479) which is occupy the other half from the middle to the bottom. however, i failed and couldn't figure out how to achieve the layout i want. How to set the webView size & Position ?

PS:如果我将布局设置为底部对齐,则Webview确实位于中间到底部的位置,但是我需要指定确切的位置..最坏的..当它加载url时,它将使用整个屏幕,覆盖了我所有的第一个布局,我迷路了.因此如何使WebView将加载的URL显示到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. so how to make the webview display the loaded url into the webview specified width and height.

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

Thank you.. sorry for my bad english.

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

anyway, this is my code (i created all programatically, i didn't use any xml layout)

    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来拆分屏幕.只需将高度GLSurfaceViewWebView都设置为高度= 0和重量= 1.在您的情况下,代码将为:

You can use LinearLayout instead of RelativeLayout to split screen. Just set height = 0 and weight = 1 of GLSurfaceView and WebView both. 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);

或者您可以对具有高度和宽度值的硬编码使用RelativeLayout:

Or you can use RelativeLayout with hardcoded height and width values:

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: 如果没有硬编码的值,则使用不可见的视图:

UPD: Without hardcoded values, using invisible view:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆