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

时间:2015-09-14 21:04:51

标签: android admob surfaceview banner-ads setcontentview

我正在努力在我的app中实现一个admob横幅,因为setContentView()方法用于名为gameView的surfaceView,因此在xml中创建adView不能应用于此框架,因为setContentView已被使用。而且我不知道如何以编程方式执行此操作。有人有解决方案吗?

我的主要活动:

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代码

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();
        }

    });
}

1 个答案:

答案 0 :(得分:1)

使用RelativeLayout或FrameLayout作为父布局,然后只定义要定位的adView的布局参数(例如,在屏幕的底部中心,如下所示):

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); 
    }
}