Displaying Ad In AndEngine

纵饮孤独 提交于 2019-11-30 14:27:29
Jong

I'm using AdMob but it should be similar.

Like @Sergey Benner referenced, you have to override onSetContentView in your activity, then create the RenderSurfaceView and your ad view manually.

First of all, create a FrameLayout to contain AndEngine's view and the ad view. Add AndEngine's view and create your ad view, then set the frame layout as the content view.

@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.
    BannerView bannerView = new BannerView(this);

    //....
    //Do any initiallizations on the banner view here.
    //....

    //Creating the banner layout params. With this params, the ad will be placed in the top of the screen, middle horizontally.
    final FrameLayout.LayoutParams bannerViewLayoutParams =
            new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
                    FrameLayout.LayoutParams.WRAP_CONTENT,
                    Gravity.TOP | Gravity.CENTER_HORIZONTAL);

    //Creating AndEngine's view.
    this.mRenderSurfaceView = new RenderSurfaceView(this);
    mRenderSurfaceView.setRenderer(mEngine, this);

    //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(bannerView, bannerViewLayoutParams);

    //Setting content view
    this.setContentView(frameLayout, frameLayoutLayoutParams);
}

Place this method in your BaseGameActivity class.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!