Xamarin 表单自适应横幅广告无法在横向加载

问题描述 投票:0回答:2

我最近从 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 在页面类的顶部声明。 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时,我意识到我犯了一个错误,那就是我这样做只是为了视图的高度请求,而我还需要为AdSize属性这样做AdMob 类本身。无论如何,这就是问题所在 - 自适应横幅广告无法应对 3200 像素宽的显示屏。

xamarin admob orientation banner-ads
2个回答
1
投票

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 包)中的一个错误,因为无法应对真实设备中所有屏幕分辨率的“自适应”广告并不名副其实。


0
投票

int adWidth = Math.Min(816, (int)(adWidthPixels / density)); adView.AdSize = AdSize.GetCurrentOrientationAnchoredAdaptiveBannerAdSize(Platform.AppContext, adWidth);

我能够使用上面的代码加载 728x90 自适应锚定广告。

© www.soinside.com 2019 - 2024. All rights reserved.