Android AdMob without XML

后端 未结 2 633
暗喜
暗喜 2021-01-25 07:44

I have a massive question to ask as I am really stuck on this and it would be create to get ads on my free application, ok first off I have been following this book

Begi

2条回答
  •  死守一世寂寞
    2021-01-25 08:02

    Create a layout container and put the AdView and the renderView in it:

    RelativeLayout layout = new RelativeLayout(this);
    AdView adView = new AdView(this, AdSize.BANNER, "a151bf25136cf46");
    layout.addView(renderView);
    layout.addView(adView);
    setContentView(layout);
    adView.loadAd(new AdRequest());
    

    EDIT 2020:

    Code above is 7 years old and the Google Mobile Ads SDK has changed a lot since then. Here's a minimal code example against the current latest SDK version.

    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);
    AdView adView = new AdView(this);
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
    adView.loadAd(new AdRequest.Builder().build());
    setContentView(layout);
    

提交回复
热议问题