Android:添加Admob横幅而不重叠主视图

时间:2011-05-23 16:52:43

标签: android admob

我正在尝试将Admob横幅添加到自定义SurfaceView(我的主游戏屏幕)。我可以将横幅放在屏幕的顶部或底部,但它在两种情况下都与游戏屏幕重叠(并隐藏了一些重要的UI元素)。如何在进入时获得横幅调整游戏屏幕大小以使其不重叠?

这个线程提供了一个xml解决方案:How to get ad to show at bottom of screen without overlap,但由于我的视图是用java编写的,我需要一种方法以编程方式完成。我尝试翻译它,但在实现代码时没有显示横幅。下面是两个解决方案,第一个没有显示任何横幅,第二个显示底部的横幅,但有重叠。我需要更改哪些参数才能摆脱重叠?

/* Admob advert on Android done programmatically! */
    adView = new AdView(this, AdSize.BANNER, "a14dc6ed8aead31");
    gameView = new GameView(this, gameEng, adView);

    //no banner displays with this code
    RelativeLayout rl = new RelativeLayout(this);
    RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
            RelativeLayout.LayoutParams.FILL_PARENT);
    RelativeLayout.LayoutParams gameViewParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, 
            RelativeLayout.LayoutParams.WRAP_CONTENT); 
    adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    gameViewParams.addRule(RelativeLayout.ABOVE);
    rl.addView(adView, adParams);
    rl.addView(gameView, gameViewParams);
    setContentView(rl);

    //a banner at the bottom displays, but it overlaps my game screen
    /*FrameLayout layout = new FrameLayout(this);
    FrameLayout.LayoutParams gameParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,
            FrameLayout.LayoutParams.FILL_PARENT);
    FrameLayout.LayoutParams adsParams =new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, 
            FrameLayout.LayoutParams.WRAP_CONTENT, android.view.Gravity.BOTTOM|android.view.Gravity.CENTER_HORIZONTAL); 
    layout.addView(gameView, gameParams);
    layout.addView(adView, adsParams);
    setContentView(layout);*/

    AdRequest request = new AdRequest();
    request.setTesting(true);
    adView.loadAd(request);

1 个答案:

答案 0 :(得分:0)

我从来没有解决这个问题,但是我找到了一个解决方案..如果有其他人遇到过这个问题,请尝试将所有绘图坐标向上移动:减去广告横幅的高度(你可以用方法getHeight())。

广告横幅广告不会立即加载,因此您必须继续检查广告横幅广告。我在主游戏循环中做到了。