如何解决Xamarin Forms AdMob 自适应横幅广告 Android
我想将 AdMob 自适应横幅广告添加到我应用的页面底部,但无论我做什么,我都无法使其正确显示。我加载广告没有问题(我的插页式广告也工作正常),但我永远无法让它正确定位。我已经尝试了一百万种代码和属性的组合,并查看了我在互联网上可以找到的所有内容,但无济于事。
我的代码分为三部分:
共享项目中的自定义视图,继承自 Frame 类(尽管互联网上有一些指导说要从 View 继承,但除非我从 Frame 继承),否则广告是完全不可见的。
using Xamarin.Forms;
namespace MyApp.Views
{
public class AdAdaptiveBannerView : Frame
{
public AdAdaptiveBannerView()
{
}
}
}
Android 项目中的自定义渲染器。
using Android.Content;
using Android.Gms.Ads;
using Android.Widget;
using System;
using Xamarin.Essentials;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(MyApp.Views.AdAdaptiveBannerView),typeof(MyApp.Droid.AdAdaptiveBanner_Droid))]
namespace MyApp.Droid
{
public class AdAdaptiveBanner_Droid : VieWrenderer<Views.AdAdaptiveBannerView,AdView>
{
public static AdView adView;
public AdAdaptiveBanner_Droid(Context context) : base(context)
{
}
private AdView CreateAdView()
{
adView = new AdView(Android.App.Application.Context)
{
AdUnitId = "ca-app-pub-3940256099942544/6300978111",//ca-app-pub-3940256099942544/6300978111 is a banner test unit.
LayoutParameters = new LinearLayout.LayoutParams(LayoutParams.WrapContent,LayoutParams.WrapContent),//For Adaptive Banner.
AdSize = AdSize.GetCurrentOrientationAnchoredAdaptiveBannerAdSize(Android.App.Application.Context,(int)(Devicedisplay.MaindisplayInfo.Width / Devicedisplay.MaindisplayInfo.Density))
//LayoutParameters = new LinearLayout.LayoutParams(LayoutParams.MatchParent,//For SmartBanner.
//AdSize = AdSize.SmartBanner
};
adView.AdListener = new ListeningRegular();
adView.LoadAd(new AdRequest.Builder().Build());
return adView;
}
protected override void OnElementChanged(ElementChangedEventArgs<Views.AdAdaptiveBannerView> e)
{
base.OnElementChanged(e);
if (e.NewElement != null && Control == null) SetNativeControl(CreateAdView());
}
}
internal class ListeningRegular : AdListener
{
public override void OnAdLoaded()
{
Console.WriteLine("Ad has loaded.");
base.OnAdLoaded();
}
public override void OnAdClosed()
{
Console.WriteLine("Ad has closed.");
AdAdaptiveBanner_Droid.adView.dispose();
AdAdaptiveBanner_Droid.adView = null;
AdAdaptiveBanner_Droid.adView.AdListener = null;
GC.Collect();
base.OnAdClosed();
}
}
}
以及共享项目中的内容页面 XAML(我将背景颜色设置为某种颜色,以便我可以更轻松地查看正在发生的事情)。
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:views="clr-MyApp.Views"
x:Class="MyApp.Pages.Page1">
<ContentPage.Content>
<StackLayout>
<Grid VerticalOptions="EndAndExpand">
<Grid.RowDeFinitions>
<RowDeFinition Height="*" />
</Grid.RowDeFinitions>
<views:AdAdaptiveBannerView BackgroundColor="DeepSkyBlue" />
</Grid>
</StackLayout>
</ContentPage.Content>
</ContentPage>
正如我所说,这可以正常加载广告,但它总是会剪掉广告的底部。除了看起来很愚蠢之外,Google 的政策还明确禁止这样做。
当打开页面时,它还会随机决定是填充屏幕宽度还是仅占用标准横幅广告宽度,如下两幅图像(取自运行 Android 7 的物理三星 galaxy S6)。
和
我认为这可能是因为 GetCurrentOrientationAnchoredAdaptiveBannerAdSize 不知道设备所处的方向并且在纵向和横向之间随机选择,但是如果我根据 Devicedisplay.MaindisplayInfo.Orientation 使用 GetLandscapeAnchoredAdaptiveBannerAdSize 和 GetPortraitAnchoredAdaptiveBannerAdSize,它仍然会随机选择。
>如果我放弃尝试使用自适应横幅,而是使用智能横幅(通过取消对上面第二个代码块中的两行注释行并注释掉上面的两行),它看起来同样糟糕,带有它周围有奇怪的边框。
它还会随机选择要加载的广告尺寸(即查看“这是 XxY 测试广告”的位置),尽管我认为这应该取决于屏幕宽度,因此应该始终相同。
我试过把它放在各种容器中(例如网格,如上面的代码所示),但没有。这篇文章已经够长了;如果我描述了我尝试过的所有内容,那就是 50 页。
很长一段时间,我的印象是它与padding有关,因为它继承自Frame(默认padding为20),但没有padding的组合(将此设置为0会使广告完全消失),边距、垂直选项、水平选项、行定义属性或任何帮助。
另外,我不知道如何让它对旋转的设备做出反应,所以这是另一个问题,因为当你旋转屏幕时它看起来不对(你只能设置一次 AdSize) .
请问有人可以帮忙吗?
编辑:我刚刚尝试以编程方式而不是在 XAML 中添加横幅广告,它给出了完全相同的结果。这太令人沮丧了。
解决方法
经过数小时的讨论,我似乎找到了解决方案。我必须使用 getCurrentOrientationAnchoredAdaptiveBannerAdSize 方法背后的代码(感谢@youssef mountassir 的代码)来设置内容页面中自适应横幅视图的高度请求。我不知道如何在渲染器中执行此操作。
它的顶部和底部仍然偶尔会出现一条细黑带,但我怀疑这与测试广告有关。
我真正想知道的是为什么我必须这样做,而其他人似乎不必这样做。
回答我的第二个问题,即旋转设备时如何更新,比较简单。我以编程方式将广告视图添加到页面,然后在设备旋转时将其替换为新视图。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。