3

我有一个 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);

    }
4

2 回答 2

3

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

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

I'm using FrameLayout in this example, it's taken from my game where FrameLayout works the best. Your onSetContentView method will be simpler - all you have to do is to instatiate the RenderSurfaceView object and the AdView object, then add them to a new LinearLayout and call setContentView.

于 2012-11-05T14:42:14.833 回答
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);
于 2012-11-05T12:01:07.463 回答