在游戏视图上方添加admob

时间:2014-06-11 14:16:59

标签: android libgdx admob

家伙!

我想在游戏视图上方添加admob,但我的广告会削减游戏视图并绘制黑色背景。

我尝试了很多方法,更改了布局的参数,但没有任何效果,感谢能够帮助我的任何人,我无法找到一种方法来完成这项工作,我很沮丧,所有的应用程序都让我看起来像。代码取自libgdx的github改变了一点我喜欢,我也尝试让它默认,但现在看起来像。希望你能帮助我并抱歉重复一些事情。谢谢你看。

抱歉我的英文不好

矿:http://i.imgur.com/06kUSHF.png 我想:http://i.imgur.com/W7ODnaw.jpg

这是我的代码:

@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();

    // Do the stuff that initialize() would do for you
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

    layout = new RelativeLayout(this);

    adView = createAdView();
    layout.addView(adView);
    View gameView = createGameView(config);
    layout.addView(gameView);

    setContentView(layout);
    startAdvertising(adView);
}

private void startAdvertising(AdView adView) {
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);
  }

private AdView createAdView() {
        adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId(SMALL_CODE);
        adView.setId(12345); // this is an arbitrary id, allows for relative positioning in createGameView()
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_BOTTOM, RelativeLayout.TRUE);
        params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
        params.addRule(RelativeLayout.ABOVE);
        layout.setLayoutParams(params);
        adView.setLayoutParams(params);
        adView.setBackgroundColor(Color.BLACK);
        return adView;
}

private View createGameView(AndroidApplicationConfiguration cfg) {
    gameView = initializeForView(new MainClass(new RequestAd(adView)), cfg);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
    params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
    params.addRule(RelativeLayout.BELOW, adView.getId());
    params.bottomMargin = 0;
    params.topMargin = 0;
    gameView.setLayoutParams(params);
    return gameView;
  }

2 个答案:

答案 0 :(得分:0)

在第二张图片(您想要的图片)中,广告显示在游戏视图下方,因此只需更改广告视图和游戏视图的参数,如下所示:

private AdView createAdView() {
        adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId(SMALL_CODE);
        adView.setId(12345); // this is an arbitrary id, allows for relative positioning in createGameView()
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_BOTTOM, RelativeLayout.TRUE);
        params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
        params.addRule(RelativeLayout.BELOW);
        layout.setLayoutParams(params);
        adView.setLayoutParams(params);
        adView.setBackgroundColor(Color.BLACK);
        return adView;
}

private View createGameView(AndroidApplicationConfiguration cfg) {
    gameView = initializeForView(new MainClass(new RequestAd(adView)), cfg);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
    params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
    params.addRule(RelativeLayout.ABOVE, adView.getId());
    params.bottomMargin = 0;
    params.topMargin = 0;
    gameView.setLayoutParams(params);
    return gameView;
  }

答案 1 :(得分:0)

首先更改视图,将adview更改为第二个,如下面onCreate

中所示
View gameView = createGameView(config);
layout.addView(gameView);
AdView admobView = createAdView();
layout.addView(admobView);

private AdView createAdView() {
    adView = new AdView(this);
    adView.setAdSize(AdSize.SMART_BANNER);
    adView.setAdUnitId("Your ad id");
    adView.setId(12345);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
    params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
    adView.setLayoutParams(params);
    return adView;
}

private View createGameView(AndroidApplicationConfiguration cfg) {
    gameView = initializeForView(new Home(this), cfg);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
    params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
    gameView.setLayoutParams(params);
    return gameView;
}
相关问题