关于原生广告,前面搬了几部分:
google admob 安卓SDK——原生广告之概述,加载和请求
今天搬原生广告的高级部分。
显示一个UnifiedNativeAd
加载原生广告后,Google Mobile Ads SDK会调用相应广告格式的监听器。 然后,您的应用负责显示广告,尽管不一定必须立即显示。 为了使显示系统定义的广告格式更加容易,SDK提供了一些有用的资源,如下所述。同时可以参考原生广告指南和原生广告政策与规范。
下面是一个创建UnifiedNativeAdView并使用UnifiedNativeAd填充它的例子:
AdLoader.Builder builder = new AdLoader.Builder(this, "<your ad unit ID>") .forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() { @Override public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) { // Assumes you have a placeholder FrameLayout in your View layout // (with id fl_adplaceholder) where the ad is to be placed. FrameLayout frameLayout = findViewById(R.id.fl_adplaceholder); // Assumes that your ad layout is in a file call ad_unified.xml // in the res/layout folder UnifiedNativeAdView adView = (UnifiedNativeAdView) getLayoutInflater() .inflate(R.layout.ad_unified, null); // This method sets the text, images and the native ad, etc into the ad // view. populateUnifiedNativeAdView(unifiedNativeAd, adView); frameLayout.removeAllViews(); frameLayout.addView(adView); } });
重点:请注意,给定原生广告的所有资产都应在UnifiedNativeAdView布局内呈现。 当在本地广告视图布局之外呈现本地资产时,Google Mobile Ads SDK尝试记录警告。
广告视图类还提供用于注册每种资产的视图的方法,以及用于注册NativeAd对象本身的方法。 通过这种方式注册视图,SDK可以自动处理以下任务:
- 记录点击
- 记录展现数(当第一个像素在屏幕上可见时)
- 显示“广告选择”叠加层
Adchoices覆盖
AdChoices覆盖层被SDK添加到每个广告视图。在你的原生广告视图的首选角落里为自动插入的AdChoices徽标留出空间。此外,重要的是,AdChoices叠加必须很容易被看到,所以记得选择适当的背景颜色和图像。有关覆盖层的外观和功能的更多信息,请参见原生广告高级字段说明。
Ad attribution
你必须显示一个广告属性来表示这个视图是一个广告。更多信息请参考admob的政策指南。
销毁广告
当你展示完你的原生广告后,你应该销毁它,这样广告就会被适当的垃圾收集。