在Android应用中加载admob广告

时间:2014-07-14 09:01:42

标签: android admob

广告中没有显示Admob广告。我的代码是

layout = (LinearLayout)findViewById(R.id.LinearLayout1);
    adView = new AdView(this);
    adView.setAdSize(com.google.android.gms.ads.AdSize.SMART_BANNER);
    adView.setAdUnitId("MY_UNIT_ID");
    adView.loadAd(new com.google.android.gms.ads.AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build());
    adView.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            // TODO Auto-generated method stub
            super.onAdLoaded();
            layout.addView(adView,0);
        }
    });

这有什么不对吗?但是当我remove来自班级的AdListener时,它就会显示出来。请帮忙。感谢

2 个答案:

答案 0 :(得分:0)

请勿尝试将AdView添加到adLoaded的布局中。只需在制作广告时添加即可。即

layout = (LinearLayout)findViewById(R.id.LinearLayout1);
adView = new AdView(this);
adView.setAdSize(com.google.android.gms.ads.AdSize.SMART_BANNER);
adView.setAdUnitId("MY_UNIT_ID");

layout.addView(adView,0);

adView.loadAd(new com.google.android.gms.ads.AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build());

答案 1 :(得分:0)

使用布局可见性进行播放

 layout = (LinearLayout)findViewById(R.id.LinearLayout1);
        adView = new AdView(this);
        adView.setAdSize(com.google.android.gms.ads.AdSize.SMART_BANNER);
        adView.setAdUnitId("MY_UNIT_ID");
        adView.loadAd(new com.google.android.gms.ads.AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build());
        layout.addView(adView);
        layout.setVisibility(View.GONE);
        adView.setAdListener(new AdListener() {
                @Override
                public void onAdLoaded() {
                    super.onAdLoaded();
                    layout.setVisibility(View.VISIBLE);
                }
            });