如何将admob广告放在LibGDX视图上方

时间:2013-12-02 21:57:38

标签: java android android-layout admob libgdx

我已经阅读了很多这样的教程:

https://github.com/libgdx/libgdx/wiki/Admob-in-libgdx

然而,我似乎无法绕过一件事。他们都使用与游戏视图重叠的广告,我宁愿它超越,我的游戏视图也只是压下来了。我怎么能这样做?我尝试过使用线性布局,但这似乎不起作用

这是我的onCreate()方法

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

        // Create the layout
        RelativeLayout layout = new RelativeLayout(this);

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

        // Create the libgdx View
        View gameView = initializeForView(new HelloWorld(), false);

        // Create and setup the AdMob view
        AdView adView = new AdView(this, AdSize.BANNER, "xxxxxxxx"); // Put in your secret key here
        adView.loadAd(new AdRequest());

        // Add the libgdx view
        layout.addView(gameView);

        // Add the AdMob view
        RelativeLayout.LayoutParams adParams = 
            new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
        adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        adParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

        layout.addView(adView, adParams);

        // Hook it all up
        setContentView(layout);
    }

2 个答案:

答案 0 :(得分:3)

如上所述here,您可以为您的游戏视图设置一个保证金,效果非常好:

    RelativeLayout layout = new RelativeLayout(this);
    RelativeLayout.LayoutParams gameViewParams =
            new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
    gameViewParams.bottomMargin = 150;

    View gameView = initializeForView(new MainGame(), cfg);
    layout.addView(gameView, gameViewParams);

    AdView adView = new AdView(this);
    adView.setAdUnitId("app-id");
    adView.setAdSize(AdSize.BANNER);

    RelativeLayout.LayoutParams adParams = 
        new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                RelativeLayout.LayoutParams.WRAP_CONTENT);
    adParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

    layout.addView(adView, adParams);

    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);

    setContentView(layout);

使用此功能,adView位于游戏视图下方,但我猜您可以放置​​marginTop并将您的游戏视图放在adView下方。

答案 1 :(得分:2)

以下是我如何处理同样的问题。我们首先要设置AdView,然后将gameView放在布局中的AdView下方。

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

    // Create the layout
    RelativeLayout layout = new RelativeLayout(this);

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

    // Create the libgdx View
    View gameView = initializeForView(new HelloWorld(), false);

    // Create and setup the AdMob view
    AdView adView = new AdView(this, AdSize.BANNER, "xxxxxxxx"); // Put in your secret key here
    adView.loadAd(new AdRequest());

    // Add the AdMob view
    RelativeLayout.LayoutParams adParams = 
        new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                RelativeLayout.LayoutParams.WRAP_CONTENT);
    adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    adParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

    //define in resource xml: <item type="id" name="adViewId"/>
    adView.setId(R.id.adViewId); 

    layout.addView(adView, adParams);

    RelativeLayout.LayoutParams gameParams = 
        new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                RelativeLayout.LayoutParams.WRAP_CONTENT);
    gameParams.addRule(RelativeLayout.BELOW, adView.getId());

    // Add the libgdx view
    layout.addView(gameView, gameParams);

    // Hook it all up
    setContentView(layout);
}