我正在尝试添加新的Facebook Ads格式。哪些是本机横幅。这些广告可以在以前的版本中使用,但是在添加新广告后便停止了。我认为问题与代码有关。
SDK:
implementation('com.facebook.android:audience-network-sdk:4.99.1') {
exclude module: 'bolts-android'
exclude group: 'com.android.support', module: 'support-annotations'
exclude group: 'com.google.android.gms'
}
布局-容器:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#000"
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/asdf"
android:layout_height="match_parent"/>
布局-广告单元:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/ad_unit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<com.facebook.ads.AdIconView
android:id="@+id/native_ad_icon"
android:layout_width="35dp"
android:layout_height="35dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<TextView
android:id="@+id/native_ad_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="1"
android:textColor="@android:color/black"
android:textSize="15sp" />
<TextView
android:id="@+id/native_ad_sponsored_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="1"
android:textColor="@android:color/darker_gray"
android:textSize="12sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/ad_choices_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:orientation="horizontal" />
</LinearLayout>
<com.facebook.ads.MediaView
android:id="@+id/native_ad_media"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:orientation="vertical">
<TextView
android:id="@+id/native_ad_social_context"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="1"
android:textColor="@android:color/darker_gray"
android:textSize="12sp" />
<TextView
android:id="@+id/native_ad_body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="center_vertical"
android:lines="2"
android:textColor="@android:color/black"
android:textSize="12sp" />
</LinearLayout>
<Button
android:id="@+id/native_ad_call_to_action"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:background="#4286F4"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:textColor="@android:color/white"
android:textSize="12sp"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>
代码:
我正在RecyclerView中加载广告。加载过程在适配器中。我正在填充列表,并向列表中的每5-6项添加一个布尔值isAd
。我正在检查是否是广告。如果是,则将发生以下情况:
final ViewGroup adView = (ViewGroup) holder.itemView;
//adCardView.removeAllViews();
mNativeAd = new NativeAd(mContext,Constants.NATIVE_HOME);
mNativeAd.setAdListener(new NativeAdListener() {
@Override
public void onMediaDownloaded(Ad ad) {
if (mNativeAd == ad) {
Log.i("FacebookAd","onMediaDownload");
}
}
@Override
public void onError(Ad ad, AdError error) {
Log.i("FacebookAd","onError");
Log.e("AdFacebook",error.getErrorMessage());
}
@Override
public void onAdLoaded(Ad ad) {
// Race condition, load() called again before last ad was displayed
if (mNativeAd == null || mNativeAd != ad) {
return;
}
// Inflate Native Ad into Container
inflateAd(mNativeAd,adView,mContext);
}
@Override
public void onAdClicked(Ad ad) {
// Ad clicked callback
}
@Override
public void onLoggingImpression(Ad ad) {
// Ad impression logged callback
}
});
try{
mNativeAd.loadAd();
} catch (Exception e){
e.printStackTrace();
}
已加载Nativ广告,但显示为No Fill
。
然后我将广告按以下方式展开:
private RelativeLayout nativeAdContainer;
private LinearLayout adView;
private void inflateAd(NativeAd nativeAd, ViewGroup adViewGroup, final Context context) {
Log.i("FacebookAd","In infalteAd()");
AdSettings.addTestDevice("7303a389-e3db-4f56-a73b-8021591b1001");
nativeAd.unregisterView();
// Add the Ad view into the ad container.
nativeAdContainer = adViewGroup.findViewById(R.id.asdf);
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate the Ad view. The layout referenced should be the one you created in the last step.
adView = (LinearLayout) inflater.inflate(R.layout.native_ad_layout, nativeAdContainer, false);
nativeAdContainer.addView(adView);
// Add the AdChoices icon
LinearLayout adChoicesContainer = adViewGroup.findViewById(R.id.ad_choices_container);
AdChoicesView adChoicesView = new AdChoicesView(context, nativeAd, true);
adChoicesContainer.addView(adChoicesView, 0);
// Create native UI using the ad metadata.
AdIconView nativeAdIcon = adView.findViewById(R.id.native_ad_icon);
TextView nativeAdTitle = adView.findViewById(R.id.native_ad_title);
MediaView nativeAdMedia = adView.findViewById(R.id.native_ad_media);
TextView nativeAdSocialContext = adView.findViewById(R.id.native_ad_social_context);
TextView nativeAdBody = adView.findViewById(R.id.native_ad_body);
TextView sponsoredLabel = adView.findViewById(R.id.native_ad_sponsored_label);
Button nativeAdCallToAction = adView.findViewById(R.id.native_ad_call_to_action);
// Set the Text.
nativeAdTitle.setText(nativeAd.getAdvertiserName());
nativeAdBody.setText(nativeAd.getAdBodyText());
nativeAdSocialContext.setText(nativeAd.getAdSocialContext());
nativeAdCallToAction.setVisibility(nativeAd.hasCallToAction() ? View.VISIBLE : View.INVISIBLE);
nativeAdCallToAction.setText(nativeAd.getAdCallToAction());
sponsoredLabel.setText(nativeAd.getSponsoredTranslation());
// Create a list of clickable views
List<View> clickableViews = new ArrayList<>();
clickableViews.add(nativeAdTitle);
clickableViews.add(nativeAdCallToAction);
// Register the Title and CTA button to listen for clicks.
nativeAd.registerViewForInteraction(
adView,
nativeAdMedia,
nativeAdIcon,
clickableViews);
}