0

我有一个表面(扩展了 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);

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

任何人都可以帮忙吗?谢谢。

4

1 回答 1

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

信用在这里

于 2013-07-08T02:32:27.750 回答