定位WebView

时间:2013-07-07 22:36:31

标签: java android webview

我有一个表面(扩展SurfaceView),一个AdView,现在我想添加一个WebView,我需要动态定位。

首先,我正在尝试在中心添加我的Web视图,并将其大小设置为设备的宽度和高度的一半。我使用800X480设备和横向模式,所以现在我想将其边框设置为left = 0,top = 120,right = 800,bottom = 360,仅仅是为了测试。稍后我会希望能够动态设置它,但到目前为止,这甚至都不起作用:

private MySurface surface;
private AdView adView;
private WebView

.
.
.

// create the ad view
adView = new AdView(this, AdSize.SMART_BANNER, AD_MOB_ID);
adView.setAdListener(new MyAdListener());

// create the surface
surface = MySurface.getRef(this);

// set a listener for touch event
surface.setOnTouchListener(this);

// create the web view

webView = new WebView(this);
webView.setWebViewClient(new WebViewClient());
webView.setVisibility(View.GONE);
LayoutParams webViewParams = new LayoutParams(800,240); // this works
webViewParams.setMargins(0, 120, 800, 360);             // this does nothing...

// create a relative layout
RelativeLayout l = new RelativeLayout(this);

// add the surface
l.addView(surface);

// add the ad view at the bottom
AdView.LayoutParams adViewParams = new AdView.LayoutParams(
AdView.LayoutParams.WRAP_CONTENT,
AdView.LayoutParams.WRAP_CONTENT);

adViewParams.addRule(AdView.ALIGN_PARENT_BOTTOM);

l.addView(adView, adViewParams);

l.addView(webView, webViewParams);

setContentView(l);  

// load an ad
//loadAdMob(); 

后来我将webview可见性设置为可见:

webView.loadUrl(url);
webView.setVisibility(View.VISIBLE);

它看起来正确的宽度和高度,但从屏幕的最顶部开始:(

有人可以帮忙吗?感谢。

1 个答案:

答案 0 :(得分:0)

好的,我想我明白了:

webView = new WebView(this);
webView.setWebViewClient(new WebViewClient());
RelativeLayout.LayoutParams webViewParams= new RelativeLayout.LayoutParams(800, 240);
webViewParams.addRule(RelativeLayout.CENTER_IN_PARENT);

RelativeLayout l = new RelativeLayout(this);
l.addView(surface);
l.addView(adView, adViewParams);
l.addView(webView, webViewParams);
setContentView(l); 

信用额度here