我在我的Xamarin.Forms上添加了AdMob广告,但Droid项目广告并未显示 而且我的项目没有任何错误 在我的AdMob帐户请求144,但广告没有显示请一些人帮助我 在控件文件夹中的Portable AdControlView中
using Xamarin.Forms;
namespace MeetupManager.Controls
{
public class AdControlView : View
{
}
}
在adView渲染器中
using Android.Widget;
using Android.Gms.Ads;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(MeetupManager.Controls.AdControlView), typeof(MeetupManager.Droid.PlatformSpecific.AdViewRenderer))]
namespace MeetupManager.Droid.PlatformSpecific
{
public class AdViewRenderer : ViewRenderer<Controls.AdControlView, AdView>
{
AdSize adSize = AdSize.SmartBanner;
AdView adView;
AdView CreateNativeAdControl()
{
if (adView != null)
return adView;
adUnitId = "My ad Unit Id";
adView = new AdView(Forms.Context);
adView.AdSize = adSize;
adView.AdUnitId = adUnitId;
var adParams = new LinearLayout.LayoutParams(LayoutParams.WrapContent, LayoutParams.WrapContent);
adView.LayoutParameters = adParams;
adView.LoadAd(new AdRequest.Builder().Build());
return adView;
}
protected override void OnElementChanged(ElementChangedEventArgs<Controls.AdControlView> e)
{
base.OnElementChanged(e);
if(Control == null)
{
CreateNativeAdControl();
SetNativeControl(adView);
}
}
}
}
在MainActivity中
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
/////////////////
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
MobileAds.Initialize(ApplicationContext, "YOUR APP ID HERE FROM AdMob, has a ~ in it");
LoadApplication(new App());
/////////////////////////
}
}
AndroidManifest.xml中的
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:label="StoryForKids.Android" android:icon="@drawable/IdeaGroup">
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
</application>
我想要的网页会显示我的广告,但不会显示
xmlns:ads="clr-namespace:MeetupManager.Controls;assembly=MeetupManager"
x:Class="MeetupManager.page2">
<ContentPage.ToolbarItems>
<ToolbarItem x:Name="english" Order="Secondary" Text="English" Icon="america.png" Clicked="english_Clicked"/>
<ToolbarItem x:Name="Kurdish" Order="Secondary" Text="Kurdish" Icon="kurdistan.png" Clicked="Kurdish_Clicked" />
</ContentPage.ToolbarItems>
<ContentPage.Content>
<StackLayout >
<Image x:Name="img" VerticalOptions="CenterAndExpand" HeightRequest="100" HorizontalOptions="Center"/>
<Label x:Name="lbltitle" HorizontalOptions="Center" FontAttributes="Bold"/>
<Label x:Name="lblstory" FontFamily="Arial"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand" />
<ads:AdControlView BackgroundColor="White" ></ads:AdControlView>
</StackLayout>
</ContentPage.Content>
答案 0 :(得分:0)
你有答案了吗?如果没有,您的问题可能是一个大小问题。我的是。在您的视图和AdViewRenderer中将广告设置为320x50,从
切换AdSize adSize = AdSize.SmartBanner;
到
AdSize adSize = AdSize.Banner;