AndEngine - Admob黑屏

时间:2012-11-05 11:50:36

标签: java android admob andengine

我有一个来自

的子类的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);

    }

2 个答案:

答案 0 :(得分:3)

它不起作用,因为在充气后更改布局。在您调用setContentView并夸大布局后,您无法添加视图。

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

我在这个例子中使用FrameLayout,它取自我的游戏FrameLayout效果最佳。您的onSetContentView方法会更简单 - 您只需要实例化RenderSurfaceView对象和AdView对象,然后将它们添加到新的LinearLayout并调用{{ 1}}。

答案 1 :(得分:1)

我认为你不能这样做

LinearLayout layout = (LinearLayout)findViewById(R.id.game_rendersurfaceview);

因为R.id.game_rendersurfaceview被声明为org.anddev.andengine.opengl.view.RenderSurfaceView

那些不是苹果/苹果。

就个人而言,我只想在XML中的主要LinearLayout中添加另一个字段来保存adView。


我不能真正具体,因为我使用广告已有几年了。您应该能够找到在XML中声明adView的示例。会是这样的

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent">

        <org.anddev.andengine.opengl.view.RenderSurfaceView android:id="@+id/game_rendersurfaceview"
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent"
            android:layout_gravity="center" />

    <adView decalaration here
android:id="@+id/game_adview" />
    </LinearLayout>

请确保并为其提供id,然后在代码中使用该ID

adView layout = (adView)findViewById(R.id.game_adview);