java-AndEngine-Admob黑屏
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-AndEngine-Admob黑屏,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3191字,纯文字阅读大概需要5分钟。
内容图文
![java-AndEngine-Admob黑屏](/upload/InfoBanner/zyjiaocheng/676/e4c6b0d21d554543b24dc40c5472229b.jpg)
我有一个AndEngine游戏,其中有子类
public class BaseActivity extends SimpleBaseGameActivity {
}
游戏运行良好,现在当我尝试向其中添加Admob视图时,我从SO上的张贴者中学到了重写onSetContentView()方法的方法.
我喜欢这样:
@Override
protected void onSetContentView() {
//Creating the parent frame layout:
final FrameLayout frameLayout = new FrameLayout(this);
//Creating its layout params, making it fill the screen.
final FrameLayout.LayoutParams frameLayoutLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.FILL_PARENT);
//Creating the banner view.
AdView adView = new AdView(this, AdSize.BANNER, AdMobPubId);
// Initiate a generic request to load it with an ad
adView.loadAd(new AdRequest());
// set the layout properties
final FrameLayout.LayoutParams adViewLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.TOP | Gravity.CENTER_HORIZONTAL);
// Creating AndEngine's view - Reference made
this.mRenderSurfaceView = new RenderSurfaceView(this);
mRenderSurfaceView.setRenderer(mEngine, null);
//createSurfaceViewLayoutParams is an AndEngine method for creating the params for its view.
final android.widget.FrameLayout.LayoutParams surfaceViewLayoutParams = new FrameLayout.LayoutParams(super.createSurfaceViewLayoutParams());
//Adding the views to the frame layout.
frameLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams);
frameLayout.addView(adView, adViewLayoutParams);
//Setting content view
this.setContentView(frameLayout, frameLayoutLayoutParams);
}
广告已加载,但屏幕为黑色.我的游戏不见了.
屏幕仍然记录触摸(如我在调试器中看到的)
为何引擎未在下部视图中显示?!?!
编辑:终于让它工作了
使用了这段代码,然后突然起作用了,不知道为什么以前没有
@Override
protected void onSetContentView() {
this.mRenderSurfaceView = new RenderSurfaceView(this);
this.mRenderSurfaceView.setRenderer(this.mEngine, this);
final android.widget.FrameLayout.LayoutParams surfaceViewLayoutParams = new FrameLayout.LayoutParams(super.createSurfaceViewLayoutParams());
//Creating the banner view.
AdView adView = new AdView(this, AdSize.BANNER, AdMobPubId);
adView.loadAd(new AdRequest());
final FrameLayout.LayoutParams adViewLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.RIGHT | Gravity.CENTER_HORIZONTAL);
final FrameLayout frameLayout = new FrameLayout(this);
final FrameLayout.LayoutParams frameLayoutLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.FILL_PARENT);
frameLayout.addView(this.mRenderSurfaceView,surfaceViewLayoutParams);
frameLayout.addView(adView,adViewLayoutParams);
this.setContentView(frameLayout, frameLayoutLayoutParams);
}
解决方法:
它不起作用,因为您需要在膨胀后更改布局.在调用setContentView并扩大布局后,您将无法添加视图.
您所要做的就是在AdEngine调用setContentView之前,先创建AdView并将其添加到布局中.您可以看到一个这样做的示例here.
在此示例中,我使用的是FrameLayout,它取自于我的游戏中FrameLayout效果最好的地方.您的onSetContentView方法将更简单-您要做的就是将RenderSurfaceView对象和AdView对象实例化,然后将它们添加到新的LinearLayout中并调用setContentView.
内容总结
以上是互联网集市为您收集整理的java-AndEngine-Admob黑屏全部内容,希望文章能够帮你解决java-AndEngine-Admob黑屏所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。