2014-08-29 98 views
0

我想覆蓋2個視圖。以編程方式覆蓋視圖RelativeLayout

我的AdView應該在我的GameView上方「浮動」。

RelativeLayout layout = new RelativeLayout(this); 
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT); 
    layout.setLayoutParams(params); 

    AdView admobView = createAdView(); 
    layout.addView(admobView); 
    View gameView = createGameView(config); 
    layout.addView(gameView); 

    setContentView(layout); 
    startAdvertising(admobView); 


private AdView createAdView() { 
    adView = new AdView(this); 
    adView.setAdSize(AdSize.SMART_BANNER); 
    adView.setAdUnitId(AD_UNIT_ID); 
    adView.setId(R.id.classic); // this is an arbitrary id, allows for relative positioning in createGameView() 
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
    params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE); 
    params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); 
    adView.setLayoutParams(params); 
    adView.setBackgroundColor(Color.TRANSPARENT); 
    return adView; 
} 

private View createGameView(AndroidApplicationConfiguration cfg) { 
    gameView = initializeForView(new MyGdxGame(), cfg); 
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); 
    params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); 
    params.addRule(RelativeLayout.BELOW, adView.getId()); 
    gameView.setLayoutParams(params); 
    return gameView; 
} 

這隻會將遊戲視圖設置在adview下面。

謝謝

編輯:太多的代碼......

+0

那麼你的問題是什麼?你在問題結束時說過'這隻會將遊戲視圖設置在adview'下面' – 2014-08-29 14:15:41

+0

希望這張圖片能夠描述我想要做的事情。我想要GameView內的AdView。 http://s1.directupload.net/images/140829/w3nkbf2s.jpg – KennyPew 2014-08-29 14:35:24

回答

1

嘗試:

private View createGameView(AndroidApplicationConfiguration cfg) { 
    gameView = initializeForView(new MyGdxGame(), cfg); 
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); 
    params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); 
    gameView.setLayoutParams(params); 
    return gameView; 
} 

更新

也改變了加料順序:

View gameView = createGameView(config); 
layout.addView(gameView); 
AdView admobView = createAdView(); 
layout.addView(admobView); 
+0

這樣的廣告不會顯示出來... – KennyPew 2014-08-29 14:50:37

+0

@KennyPew你首先添加到佈局的視圖是什麼?嘗試先添加'gameView',然後添加'adView'。 – 2014-08-29 15:03:53

+0

AdView,我編輯了我的第一篇文章 – KennyPew 2014-08-29 15:06:36

相關問題