2013-07-07 55 views
0

我有一個表面(它擴展了SurfaceView),一個AdView,現在我想添加一個WebView,我將需要動態定位。定位WebView

首先,我試圖在中心添加我的web視圖,並將其大小設置爲設備的寬度和高度的一半。我使用的是800X480設備和橫向模式,所以現在我想在左側= 0,頂部= 120,右側= 800,底部= 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); 

看樣子正確的寬度和高度,但在開始屏幕:(

誰能幫助的最頂端感謝

+0

這也沒有什麼影響:webViewParams.gravity = Gravity.CENTER; –

回答

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