如何在安卓应用项目设置audiencenetwork原生广告模板
如果你熟悉操作,或正在使用Audience Network SDK,则只需额外两步:添加新的支持库,并将模板添加到代码。如果你是首次设置或想回顾详细步骤,请参考以下步骤:
1:将Audience Network SDK添加到应用Gradle
dependencies {
…
pile '.facebook.android:audience-network-sdk:4.+'
}
2:将原生广告模板库添加到库文件夹
在Facebook开源项目中下载库“audience-network-support.jar”,并将其复制到项目的库文件夹中,确保您的Gradle包含下列行:
dependencies {
pile fileTree(dir: 'libs', include: ['*.jar'])
…
}
添加完成后,请同步项目以体现相关更改。
3:实施代码
在活动类别顶部添加下列代码,以便导入支持库
import .facebook.audiencenetwork.ads.audience_network
_support.NativeAdTemplateView;
添加下列行,使用模板创建广告
// create ad view
NativeAdTemplateView adView = new NativeAdTemplateView(this,
"808505382626354_823834164426809",
NativeAdTemplateView.Type.FB_BIG_CIRCLE,
getCustomizedAttributes());
例如,完整代码将如下所示,
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// create ad view
NativeAdTemplateView adView = new NativeAdTemplateView(this,
"808505382626354_823834164426809",
NativeAdTemplateView.Type.FB_BIG_CIRCLE,
getCustomizedAttributes());
// place ad view in main UI
placeAdInView(adView);
}
protected void placeAdInView(NativeAdTemplateView adView) {
RelativeLayout iconContainer = (RelativeLayout) findViewById(R.id.ad_container);
iconContainer.addView(adView);
}
protected JSONObject getCustomizedAttributes() {
JSONObject prefers = new JSONObject();
if (CUSTOMIZE_TEMPLATE) {
// customize icon and title
try {
prefers.put("stock_image", true);
prefers.put("stock_resource", getResources().getIdentifier("icon_purple", "drawable", getPackageName()));
} catch (JSONException e) {
e.printStackTrace();
}
}
return prefers;
}
https://ds.fanyaozu.com/tag/%e7%bb%99%e7%94%b5%e5%95%86%e6%96%b0%e6%89%8b%e7%9a%84%e5%bb%ba%e8%ae%ae