您好我使用下面的代码在我的游戏中展示广告并且工作正常,但我在setContentView()
中创建了此横幅(GameActivity.java
)。我有gameScene.java
。我有时想隐藏这个横幅。那么如何从gameScene.java到达“adView”变量(在GameActivity.java中)?
我有一个活动GameActivity.java和其他场景(gameScene,menuScene ......)。 我想在菜单场景中显示横幅,但我也想在游戏场景中隐藏它。
@Override
@SuppressLint("NewApi")
protected void onSetContentView() {
super.onSetContentView();
final FrameLayout frameLayout = new FrameLayout(this);
final FrameLayout.LayoutParams frameLayoutLayoutParams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT, Gravity.FILL);
final FrameLayout.LayoutParams adViewLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.LEFT | Gravity.TOP);
adView = new AdView(this);
adView.setAdUnitId("XXXXXXXXXXXXXX");
adView.setAdSize(AdSize.BANNER);
adView.setVisibility(AdView.VISIBLE);
adView.refreshDrawableState();
AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
adView.loadAd(adRequest);
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
adView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
this.mRenderSurfaceView = new RenderSurfaceView(this);
mRenderSurfaceView.setRenderer(mEngine, this);
final FrameLayout.LayoutParams surfaceViewLayoutParams = new FrameLayout.LayoutParams(
android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.MATCH_PARENT);
surfaceViewLayoutParams.gravity = Gravity.CENTER;
frameLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams);
frameLayout.addView(adView, adViewLayoutParams);
this.setContentView(frameLayout, frameLayoutLayoutParams);
}
答案 0 :(得分:0)
如果您想隐藏它,只需致电adView.setVisiblity(View.GONE)
即可。如果您想再次展示,请拨打adView.setVisiblity(View.VISIBLE)
。
答案 1 :(得分:0)
像Alesandro建议的那样。
将对Activity的引用传递给GameScene,并使用该引用调用findViewById来获取AdView。
答案 2 :(得分:0)
SkinWalker,我在AndEngine论坛上引用了你的帖子,在这里,我遇到了同样的问题,但是Androidacct为我解决了这个问题。阅读这篇文章:Hide/Show AdView from Scene
我希望它可以帮到你!
修改强>
这就是它对我的帮助: 在BaseGameActivity中添加以下代码:
public void showAds() {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
adView.setVisibility(View.VISIBLE);
AdRequest adRequest = new AdRequest.Builder().addTestDevice(
AdRequest.DEVICE_ID_EMULATOR).build();
adView.loadAd(adRequest);
}
});
}
public void hideAds() {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
adView.setVisibility(View.INVISIBLE);
}
});
}
您必须使用以下代码从场景中调用它:
ResourcesManager.getInstance().activity.hideAds();
我建议您阅读本教程:Full game tutorial - part 3. Resources Manager
最诚挚的问候!