gpt4 book ai didi

java - AndEngine - Admob黑屏

转载 作者:太空宇宙 更新时间:2023-11-03 13:31:03 24 4
gpt4 key购买 nike

我有一个 AndEngine 游戏,它的子类来自

public class BaseActivity extends SimpleBaseGameActivity {

}

游戏运行良好,现在当我尝试向其中添加 Admob View 时,我从 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);
}

广告已加载,但屏幕是黑色的。我的游戏没了屏幕仍然记录触摸(正如我在调试器中看到的那样)

为什么在下 View 中没有显示引擎?!?!

编辑:终于让它工作了

用过这段代码,突然就可以用了,不知道为什么之前不行

@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 并扩充布局后,您无法添加 View 。

您所要做的就是在 AndEngine 调用 setContentView 之前先创建 AdView 并将其添加到布局中。您可以看到这样做的示例 here .

我在这个例子中使用了 FrameLayout,它取 self 的游戏,其中 FrameLayout 效果最好。您的 onSetContentView 方法会更简单 - 您所要做的就是实例化 RenderSurfaceView 对象和 AdView 对象,然后将它们添加到一个新的LinearLayout 并调用 setContentView

关于java - AndEngine - Admob黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13231536/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com