如何改变android banner app的位置

时间:2014-12-08 22:20:03

标签: android ads

我能够在我的libgdx Android应用程序上添加横幅广告,但我需要一些帮助定位添加。现在,如果我运行它,而不是横幅广告显示在顶部/中央,它会移动应用程序屏幕的其余部分。

我如何更改它,以便横幅广告只是浮动在顶部/中心而不会移动应用程序屏幕?

    public class AndroidLauncher extends AndroidApplication {
        // Ads
        private static final String AD_UNIT_ID_BANNER  = "ca-app-pub-222222222/111111";
        private static final String GOOGLE_PLAY_URL = "https://play.google.com/store/apps/developer?id=testname";
        protected AdView adView;
        protected View gameView;

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

            RelativeLayout layout = new RelativeLayout(this);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT);
            layout.setLayoutParams(params);

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

            setContentView(layout);
            startAdvertising(admobView);

            //initialize(new MyGdxGame(), config);
        }

        private AdView createAdView() {
            adView = new AdView(this);
            adView.setAdSize(AdSize.SMART_BANNER);
            adView.setAdUnitId(AD_UNIT_ID_BANNER);
            adView.setId(12345); // this is an arbitrary id, allows for relative
                                // positioning in createGameView()
            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);
            adView.setLayoutParams(params);
            adView.setBackgroundColor(Color.BLACK);
            return adView;
    }

    private View createGameView(AndroidApplicationConfiguration cfg) {
            gameView = initializeForView(new MyGdxGame(), 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());
        gameView.setLayoutParams(params);
        return gameView;
    }

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

1 个答案:

答案 0 :(得分:1)

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

中所示

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

从createGameView函数中删除或隐藏此行

params.addRule(RelativeLayout.BELOW, adView.getId());