java-AndEngine-Admob黑屏
作者:互联网
我有一个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.
标签:andengine,admob,java,android 来源: https://codeday.me/bug/20191031/1977646.html