在使用andengine制作的安卓游戏中,Admob横幅广告显示高度不正确

时间:2014-03-07 11:39:30

标签: android android-layout admob andengine

我已经实现了admob | GoogleMobileAdSDK进入我的Android游戏构建与andengine。广告正在加载到游戏中,但在打开应用后第一次无法正确显示。但是,当我退出应用程序活动时,通过单击添加或去谷歌播放排行榜并回到应用程序然后它开始正确显示它。 所以基本上当我打开应用程序时,广告的全宽,但几乎是一半的高度。如果点击广告,请转到广告链接,然后按回按钮返回我的应用,广告的全宽全高。坚持这3天:/

public static AdView adView;
@Override
protected void onSetContentView() {

    final FrameLayout frameLayout = new FrameLayout(this);
    final FrameLayout.LayoutParams frameLayoutLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
    adView = new AdView(this);
    new AdView(this);
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId("MY AD UNIT ID");
    final FrameLayout.LayoutParams adViewLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.RIGHT | Gravity.CENTER_HORIZONTAL);
    this.mRenderSurfaceView = new RenderSurfaceView(this);
    mRenderSurfaceView.setRenderer(this.mEngine, this);

    AdRequest adRequest = new AdRequest.Builder()
    .addTestDevice("MY EMULATOR ID")
    .addTestDevice("MY DEVICE ID")
    .build();

    final FrameLayout.LayoutParams surfaceViewLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
    frameLayout.addView(this.mRenderSurfaceView,surfaceViewLayoutParams);
    frameLayout.addView(adView,adViewLayoutParams);
    this.setContentView(frameLayout, frameLayoutLayoutParams);

    adView.loadAd(adRequest);
    Log.d("Ads","AdView banner size is "+adView.getWidth()+" and "+adView.getHeight());
    //Creating the banner view.

}

此外,我还会将代码输入代码以查看广告尺寸。它始终在应用程序的开头打印出来

  

AdView横幅尺寸为0和0

1 个答案:

答案 0 :(得分:2)

尝试在加载后adView添加frameLayout

adView.setAdListener(new AdListener() {
    public void onAdLoaded(){
        frameLayout.addView(adView, adViewLayoutParams);
    }
});

如果您的广告令人耳目一新,您可能需要先检查adView是否已添加到frameLayout,然后再添加,否则会崩溃。