当 setContentView() 用于 Surfaceview 时实现 Admob 横幅

Implementing Admob banner when setContentView() is used for the Surfaceview(当 setContentView() 用于 Surfaceview 时实现 Admob 横幅)

本文介绍了当 setContentView() 用于 Surfaceview 时实现 Admob 横幅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力在我的应用程序中实现 admob 横幅,因为 setContentView() 方法用于名为 gameView 的 surfaceView,因此无法将在 xml 中创建的 adView 应用于此框架,因为 setContentView 已被使用.而且我不知道如何以编程方式执行此操作.有没有人可以解决这个问题?

I am struggling to implement an admob banner into my app because the setContentView() method is used for the surfaceView called gameView so creating the adView in xml cannot be applied to this framework as setContentView is already being used. And I don't know how to do this programmatically. Does anyone have a solution to this?

我的主要活动:

public class GameMainActivity extends BaseGameActivity {
....
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    instance = this;
    prefs = getPreferences(Activity.MODE_PRIVATE); // New line!
    highScore = retrieveHighScore();
    highScoreUnits = retrieveHighScoreUnits();
    highScoreTens = retrieveHighScoreTens();
    highScoreHundreds = retrieveHighScoreHundreds();
    muteButton = retrieveMuteButton();
    assets = getAssets();
    sGame = new GameView(this, GAME_WIDTH, GAME_HEIGHT);
    setContentView(sGame);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

}

还有我的自定义surfaceView代码

and my custom surfaceView code

public class GameView extends SurfaceView implements Runnable {

private Bitmap gameImage;
private Rect gameImageSrc;
private Rect gameImageDst;
private Canvas gameCanvas;
private Painter graphics;

private Thread gameThread;
private volatile boolean running = false;
private volatile State currentState;

private InputHandler inputHandler;


public GameView(Context context, int gameWidth, int gameHeight) {
    super(context);
    gameImage = Bitmap.createBitmap(gameWidth, gameHeight,
            Bitmap.Config.RGB_565);
    gameImageSrc = new Rect(0, 0, gameImage.getWidth(),
            gameImage.getHeight());
    gameImageDst = new Rect();
    gameCanvas = new Canvas(gameImage);
    graphics = new Painter(gameCanvas);
    SurfaceHolder holder = getHolder();
    holder.addCallback(new Callback() {

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            initInput();
            if (currentState == null) {
                setCurrentState(new LoadState());
            }
            initGame();
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format,
                                   int width, int height) {
            // TODO Auto-generated method stub
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            pauseGame();
        }

    });
}

推荐答案

使用RelativeLayout或者FrameLayout作为你的父布局,然后定义adView要定位的布局参数(例如在屏幕底部中心像这样):

Use a RelativeLayout or a FrameLayout as your parent layout, then just define the layout parameters for the adView to be positioned (for example at the bottom center of the screen like this):

public class GameMainActivity extends BaseGameActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        instance = this;
        prefs = getPreferences(Activity.MODE_PRIVATE); // New line!
        highScore = retrieveHighScore();
        highScoreUnits = retrieveHighScoreUnits();
        highScoreTens = retrieveHighScoreTens();
        highScoreHundreds = retrieveHighScoreHundreds();
        muteButton = retrieveMuteButton();
        assets = getAssets();
        // Create an ad.
        AdView adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId(AD_UNIT_ID);
        // set background color of adview to force it to show
        adView.setBackgroundColor(Color.TRANSPARENT);
        // Add the AdView to the view hierarchy. The view will have no size
        // until the ad is loaded.
        RelativeLayout layout = new RelativeLayout(this);
        layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
        // Create an ad request.
        AdRequest adRequest = new AdRequest.Builder().build();

        // Start loading the ad in the background.
        adView.loadAd(adRequest);

        // Request full screen
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

        // Create and set your game's view
        sGame = new GameView(this, GAME_WIDTH, GAME_HEIGHT);


        RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

        adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        layout.addView(sGame);
        layout.addView(adView, adParams);
        setContentView(layout);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
    }
}

这篇关于当 setContentView() 用于 Surfaceview 时实现 Admob 横幅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:当 setContentView() 用于 Surfaceview 时实现 Admob 横幅

基础教程推荐