How do I add the admob view when my main view is l

2019-09-05 23:35发布

I'm looking at this example, but here the code wants to use some layout file, but I dont have that in my code..

http://code.google.com/intl/da/mobile/ads/docs/android/fundamentals.html

My activity looks like this.. I have no clue how to add the admob view to the view I use here..

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    GameView vw = new GameView( this, intDpi );

    setContentView(vw);

}

标签: view admob
1条回答
冷血范
2楼-- · 2019-09-06 00:14

You can create a layout above the GameView and put the GameView and the AdView into this layout. The below example uses a RelativeLayout and puts the add at the bottom of the screen.

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  RelativeLayout layout = new RelativeLayout(this);
  GameView vw = new GameView( this, intDpi );
  layout.addView(vw);

  adView = new AdView(this, AdSize.BANNER, "YOUR_AD_UNIT_ID");
  // Places adView at bottom of screen.
  RelativeLayout.LayoutParams params =
      new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                                      RelativeLayout.LayoutParams.WRAP_CONTENT);
  params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
  layout.addView(adView);
  adView.loadAd(new AdRequest());

  setContentView(layout);
}
查看更多
登录 后发表回答