0

我使用以下示例在我的应用程序的场景中添加了 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 也会发生这种情况。

使用带有 AddRule 的 RelativeLayout:

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

1 回答 1

0

我在我的应用程序中以这种方式使用 AdMob 广告,它对我有用(广告位于屏幕底部):

 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 方法,但我仍在使用它。

于 2017-01-28T08:07:30.897 回答