在 Android 應(yīng)用項(xiàng)目中設(shè)置Facebook audience network原生廣告模板的詳細(xì)步驟是什么?
如果你熟悉操作,或正在使用 Audience Network SDK,則只需額外兩步:添加新的支持庫(kù),并將模板添加到代碼。如果你是首次設(shè)置或想回顧詳細(xì)步驟,請(qǐng)參考以下步驟:
1:將 Audience Network SDK 添加到應(yīng)用 Gradle
dependencies {
...
compile 'com.facebook.android:audience-network-sdk:4.+'
}
2:將原生廣告模板庫(kù)添加到庫(kù)文件夾
在 Facebook 開(kāi)源項(xiàng)目中下載庫(kù)“audience-network-support.jar”,并將其復(fù)制到項(xiàng)目的庫(kù)文件夾中,確保您的 Gradle 包含下列行:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
...
}
添加完成后,請(qǐng)同步項(xiàng)目以體現(xiàn)相關(guān)更改。
3:實(shí)施代碼
在活動(dòng)類別頂部添加下列代碼,以便導(dǎo)入支持庫(kù)
import com.facebook.audiencenetwork.ads.audience_network
_support.NativeAdTemplateView;
添加下列行,使用模板創(chuàng)建廣告
// 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;
}