【发布时间】:2012-11-05 11:50:36
【问题描述】:
我有一个 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);
}
【问题讨论】:
-
哦,别忘了,我添加了 admob sdk,进行了构建路径和命令导出,一切就绪,一切顺利......只是黑屏......
-
为了确定,尝试将
android:id="@+id/adView"更改为android:id="@+id/game_adView"之类的东西
标签: java android admob andengine