How do I add the admob view when my main view is like this?

风流意气都作罢 提交于 2019-12-24 22:42:56

问题


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);

}

回答1:


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);
}


来源:https://stackoverflow.com/questions/9300459/how-do-i-add-the-admob-view-when-my-main-view-is-like-this

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!