java-AndEngine-Admob黑屏

java-AndEngine-Admob黑屏,第1张

概述我有一个AndEngine游戏,其中有子类publicclassBaseActivityextendsSimpleBaseGameActivity{}游戏运行良好,现在当我尝试向其中添加Admob视图时,我从SO上的张贴者中学到了重写onSetContentView()方法的方法.我喜欢这样:@OverrideprotectedvoidonSetContentView(){

我有一个AndEngine游戏,其中有子类

public class BaseActivity extends SimpleBaseGameActivity {}

游戏运行良好,现在当我尝试向其中添加Admob视图时,我从SO上的张贴者中学到了重写onSetContentVIEw()方法的方法.

我喜欢这样:

@OverrIDeprotected 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黑屏所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1086210.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存