将Admob横幅放置在屏幕底部

时间:2017-01-28 01:49:37

标签: c# cocos2d-x cocos2d-x-3.0 cocossharp

我使用以下示例添加了Admob横幅广告到我的应用中的一个场景:https://forums.xamarin.com/discussion/51779/cocossahrp-and-admob

广告显示在屏幕顶部,但我想在底部显示。我找不到任何CocosSharp示例,但我找到了一个cocos2d-x(最后一篇文章):http://discuss.cocos2d-x.org/t/admob-bottom-placement-tutorial-v3-0/13854/11

我已尝试使用我的代码(见下文),但无法使其正常运行。我注意到的是,如果我将LayoutParams从200更改为size.Y广告横幅显示在屏幕中间而不是顶部,但即使不使用AlignParentBottom或Gravity.Bottom,也会发生这种情况。

将RelativeLayout与AddRule一起使用:

        Android.Graphics.Point size = new Android.Graphics.Point();
        prActivity.WindowManager.DefaultDisplay.GetSize(size);
        RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(
            size.X,
            200);
        var bannerAd = new AdView(prActivity);
        bannerAd.AdSize = AdSize.SmartBanner;
        bannerAd.AdUnitId = "ca-app-pub-3940256099942544/6300978111"; // Test UnitId
        bannerAd.LayoutParameters = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
        var lcRequestbuilder = new AdRequest.Builder();
        bannerAd.LoadAd(lcRequestbuilder.Build());
        adParams.AddRule(LayoutRules.AlignParentBottom); // Rule to place ad at the bottom
        prActivity.AddContentView(bannerAd, adParams);

        ScheduleOnce(t =>
        {
            bannerAd.BringToFront();
        }, 3f);

使用具有重力的LinearLayout:

        Android.Graphics.Point size = new Android.Graphics.Point();
        prActivity.WindowManager.DefaultDisplay.GetSize(size);
        LinearLayout.LayoutParams adParams = new LinearLayout.LayoutParams(
            size.X,
            200);
        var bannerAd = new AdView(prActivity);
        bannerAd.AdSize = AdSize.SmartBanner;
        bannerAd.AdUnitId = "ca-app-pub-3940256099942544/6300978111"; // Test UnitId
        bannerAd.LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
        var lcRequestbuilder = new AdRequest.Builder();
        bannerAd.LoadAd(lcRequestbuilder.Build());
        adParams.Gravity = GravityFlags.Bottom; // Gravity Flag to place ad at the bottom
        prActivity.AddContentView(bannerAd, adParams);

        ScheduleOnce(t =>
        {
            bannerAd.BringToFront();
        }, 3f);

1 个答案:

答案 0 :(得分:0)

我在我的应用中以这种方式使用AdMob Ad并且它为我工作(广告位于屏幕的底部):

 private static final String AD_UNIT_ID = "";
 private AdView adView;

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

    LinearLayout.LayoutParams adParams = new LinearLayout.LayoutParams(getWindowManager().getDefaultDisplay().getWidth(), (getWindowManager().getDefaultDisplay().getHeight() + getWindowManager().getDefaultDisplay().getHeight()) - 100);
        adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId(AD_UNIT_ID);
        adView.setBackgroundColor(Color.TRANSPARENT);
        addContentView(this.adView, adParams);

    }

不推荐使用Display的getWidth方法,但我仍在使用它。