Android: adding Admob banner without overlapping main view
I'm trying to add an Admob banner to a custom SurfaceView (my main game screen). I can put the banner at the top or bottom of the screen, but it overlaps the game screen in both cases (and hides some essential UI elements). How can I get the banner to resize the game screen upon entry so that it doesn't overlap anything?
This thread gives an xml solution: How to get ad to show at bottom of screen without overlap, but since my view is written in java, I need a way to do it programmatically. I attempted to translate it, but no banner shows up when I implement the code. Below are two solutions, the first doesn't show any banner, and the second shows a banner at the bottom, but with overlap. What parameters do I need to change to get rid of the overlap?
/* Admob advert on Android done programmatically! */
adView = new AdView(this, AdSize.BANNER, "a14dc6ed8aead31");
gameView = new GameView(this, gameEng, adView);
//no banner displays with this code
RelativeLayout rl = new RelativeLayout(this);
RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
RelativeLayout.LayoutParams gameViewParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
gameViewParams.addRule(RelativeLayout.ABOVE);
rl.addView(adView, adParams);
rl.addView(gameView, gameViewParams);
setContentView(rl);
//a banner at the bottom displays, but it overlaps my game screen
/*FrameLayout layout = new FrameLayout(this)开发者_运维知识库;
FrameLayout.LayoutParams gameParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,
FrameLayout.LayoutParams.FILL_PARENT);
FrameLayout.LayoutParams adsParams =new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT, android.view.Gravity.BOTTOM|android.view.Gravity.CENTER_HORIZONTAL);
layout.addView(gameView, gameParams);
layout.addView(adView, adsParams);
setContentView(layout);*/
AdRequest request = new AdRequest();
request.setTesting(true);
adView.loadAd(request);
I never solved this, but I found a work around.. In case anyone else ever runs into this problem, try to just move all your drawing coordinates up: subtract the height of the ad banner (you can get that with the method getHeight()).
The ad banner doesn't load right away though, so you'll have to keep checking for it. I did that in my main game loop.
精彩评论