繁体   English   中英

Xamarin Forms 自适应横幅广告不会在横向加载

[英]Xamarin Forms Adaptive Banner Ad Won't Load in Landscape

我最近从 Galaxy S6 升级到 Galaxy S21 Ultra,现在 AdMob 自适应横幅广告无法横向加载。 在 S6 上没问题,但在 S21 上我收到错误消息:

[广告] 广告加载失败:1

我用来创建广告的代码在这里:

private void NewAdaptiveBannerAd()
{
    try
    {
        adAdaptiveBannerView = new AdAdaptiveBannerView();
        adAdaptiveBannerView.Padding = new Thickness(0);
        adAdaptiveBannerView.BackgroundColor = (Color)Application.Current.Resources["baseBackground_Light"];

        AdGrid.Children.Add(adAdaptiveBannerView, 0, 1);

        adAdaptiveBannerView.HeightRequest = getCurrentOrientationAnchoredAdaptiveBannerAdSize(Android.App.Application.Context, (int)(DeviceDisplay.MainDisplayInfo.Width / DeviceDisplay.MainDisplayInfo.Density));

        static int getCurrentOrientationAnchoredAdaptiveBannerAdSize(Context context, int adWidth)
        {
            int var3 = 0;

            if (context == null)
            {
                return 0;
            }
            else
            {
                int var7 = 2;
                switch (DeviceDisplay.MainDisplayInfo.Orientation)
                {
                    case DisplayOrientation.Unknown: var7 = 0; break;
                    case DisplayOrientation.Portrait: var7 = 1; break;
                    case DisplayOrientation.Landscape: var7 = 2; break;
                    default: return 0;
                }
                if (var3 == 0)
                {
                    var3 = var7;
                }

                int var8 = var3 == var7 ? (int)Math.Round((float)DeviceDisplay.MainDisplayInfo.Height / DeviceDisplay.MainDisplayInfo.Density) : (int)Math.Round((float)DeviceDisplay.MainDisplayInfo.Width / DeviceDisplay.MainDisplayInfo.Density);
                int var9 = (int)Math.Min(90, Math.Round((float)var8 * 0.15F));
                int var10;
                if (adWidth > 655)
                {
                    var10 = (int)Math.Round((float)adWidth / 728.0F * 90.0F);
                }
                else if (adWidth > 632)
                {
                    var10 = 81;
                }
                else if (adWidth > 526)
                {
                    var10 = (int)Math.Round((float)adWidth / 468.0F * 60.0F);
                }
                else if (adWidth > 432)
                {
                    var10 = 68;
                }
                else
                {
                    var10 = (int)Math.Round((float)adWidth / 320.0F * 50.0F);
                }

                var10 = Math.Max(Math.Min(var10, var9), 50);
                return var10;
            }
        }
    }
    catch (Exception)
    {
        Console.WriteLine("Creation of banner ad in SavedCharts failed.");
    }
}

adAdaptiveBannerView 在 pages 类的顶部声明。 getCurrentOrientationAnchoredAdaptiveBannerAdSize 函数来自这里的另一篇文章,我现在找不到了。

我找不到任何可以解释这一点的东西,或者为什么这两款“手机”之间的行为会有所不同(是 Android 版本的差异、不同的纵横比还是什么?)。 在模拟器上进行测试很困难,因为它没有正确报告方向。

有谁知道发生了什么?

我的应用程序本质上主要用于横向模式,所以这个问题意味着很多人可能看不到广告。

PS 我使用的是 Xamarin.GooglePlayServices.Ads 版本 119.8.0。 我一直无法实现 120 版,因为没有文档或任何可以帮助我做到的东西。

编辑 1:我尝试在纵向时有条件地添加自适应横幅广告,以及使用 MarcTron.Admob 的标准横幅广告。 它不起作用。

我在想是不是因为S21 Ultra的显示屏是1440 x 3200像素,纵横比为20:9,Google Ad Mob应付不来。 如果是这种情况,那就太愚蠢了(“自适应”?!)。 所以我试着给它一个 2560 的屏幕宽度,因为它可以在 S6(1440x3200)和 1600(即一半)上工作,但都没有工作。

编辑 2:当我尝试将屏幕宽度限制为 2560px 时,我意识到我犯了一个错误,即我只对视图的高度请求执行此操作,而我还需要为 AdMob 的 AdSize 属性执行此操作类本身。 无论如何,这就是问题所在——自适应横幅广告无法处理 3200 像素宽的显示器。

Google AdMab 自适应横幅广告无法处理 3200 像素的屏幕宽度。 我不知道实际的限制是什么,但由于它适用于我的其他设备,屏幕宽度为 2560 像素,因此我将其限制为:

adView.AdSize = AdSize.GetCurrentOrientationAnchoredAdaptiveBannerAdSize(Android.App.Application.Context, (int)(Math.Min(2560,DeviceDisplay.MainDisplayInfo.Width) / DeviceDisplay.MainDisplayInfo.Density));

并将相同的 Math.Min() 限制器应用于我问题中的代码。

横幅广告(3200-2560)/2 = 320 像素宽的任何一侧的间隙都有一点不幸的副作用。 这并不违反 Google 的政策( https://developers.google.com/admob/android/banner/adaptive )。

我认为这是 Google 的 API(或至少是 NuGet 包)中的一个错误,因为无法处理真实设备中所有屏幕分辨率的“自适应”广告并不符合其名称。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM