Android:添加 Admob 横幅而不重叠主视图
我正在尝试将 Admob 横幅添加到自定义 SurfaceView(我的主游戏屏幕)。我可以将横幅放在屏幕的顶部或底部,但在这两种情况下它都会与游戏屏幕重叠(并隐藏一些基本的 UI 元素)。如何让横幅在进入时调整游戏屏幕大小,使其不与任何内容重叠?
该线程提供了一个 xml 解决方案:如何让广告在屏幕底部显示而不重叠,但由于我的视图是用 java 编写的,所以我需要一种以编程方式执行此操作的方法。我尝试翻译它,但当我实现代码时没有出现横幅。下面是两个解决方案,第一个不显示任何横幅,第二个在底部显示横幅,但有重叠。我需要更改哪些参数才能消除重叠?
/* 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'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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我从未解决过这个问题,但我找到了解决方法。如果其他人遇到这个问题,请尝试将所有绘图坐标向上移动:减去广告横幅的高度(您可以使用方法 getHeight( ))。
不过,广告横幅不会立即加载,因此您必须继续检查。我在主游戏循环中做到了这一点。
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.