我按照说明https://developers.facebook.com/docs/audience-network/android-native在我的应用中实现了nativeAd。根据此示例,示例广告应如下所示
我看起来像这样
MainActivity
private NativeAd nativeAd;
private LinearLayout nativeAdContainer;
private LinearLayout adView;
private void showNativeAd() {
nativeAd = new NativeAd(this, "placement id");
nativeAd.setAdListener(new AdListener() {
@Override
public void onError(Ad ad, AdError error) {
// Ad error callback
}
@Override
public void onAdLoaded(Ad ad) {
// Ad loaded callback
if (nativeAd != null) {
nativeAd.unregisterView();
}
// Add the Ad view into the ad container.
nativeAdContainer = (LinearLayout) findViewById(R.id.native_ad_container);
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
// 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);
// Create native UI using the ad metadata.
ImageView nativeAdIcon = (ImageView) adView.findViewById(R.id.native_ad_icon);
TextView nativeAdTitle = (TextView) adView.findViewById(R.id.native_ad_title);
MediaView nativeAdMedia = (MediaView) adView.findViewById(R.id.native_ad_media);
TextView nativeAdSocialContext = (TextView) adView.findViewById(R.id.native_ad_social_context);
TextView nativeAdBody = (TextView) adView.findViewById(R.id.native_ad_body);
Button nativeAdCallToAction = (Button) adView.findViewById(R.id.native_ad_call_to_action);
// Set the Text.
nativeAdTitle.setText(nativeAd.getAdTitle());
nativeAdSocialContext.setText(nativeAd.getAdSocialContext());
nativeAdBody.setText(nativeAd.getAdBody());
nativeAdCallToAction.setText(nativeAd.getAdCallToAction());
// Download and display the ad icon.
NativeAd.Image adIcon = nativeAd.getAdIcon();
NativeAd.downloadAndDisplayImage(adIcon, nativeAdIcon);
// Download and display the cover image.
nativeAdMedia.setNativeAd(nativeAd);
// Add the AdChoices icon
LinearLayout adChoicesContainer = (LinearLayout) findViewById(R.id.ad_choices_container);
AdChoicesView adChoicesView = new AdChoicesView(MainActivity.this, nativeAd, true);
adChoicesContainer.addView(adChoicesView);
// Register the Title and CTA button to listen for clicks.
List<View> clickableViews = new ArrayList<>();
clickableViews.add(nativeAdTitle);
clickableViews.add(nativeAdCallToAction);
nativeAd.registerViewForInteraction(nativeAdContainer, clickableViews);
}
@Override
public void onAdClicked(Ad ad) {
// Ad clicked callback
}
@Override
public void onLoggingImpression(Ad ad) {
// Ad impression logged callback
}
});
// Request an ad
nativeAd.loadAd();
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="me.skyse.disablefingerprint.MainActivity">
<LinearLayout
android:id="@+id/native_ad_container"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:orientation="vertical"/>
<include layout="@layout/native_ad_layout" />
</LinearLayout>
navtive_ad_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/native_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">
<LinearLayout
android:id="@+id/ll_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<ImageView
android:id="@+id/native_ad_icon"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_gravity="center_vertical"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="5dp">
<TextView
android:id="@+id/native_ad_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:textColor="@android:color/black"
android:textSize="15sp"/>
<TextView
android:id="@+id/sponsored_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="1"
android:text="Sponsored"
android:textColor="@android:color/darker_gray"
android:textSize="10sp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/ad_choices_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal"/>
</LinearLayout>
<com.facebook.ads.MediaView
android:id="@+id/native_ad_media"
android:layout_width="wrap_content"
android:layout_height="100dp"
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"
android:paddingRight="10dp">
<TextView
android:id="@+id/native_ad_social_context"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="center_vertical"
android:lines="2"
android:paddingRight="5dp"
android:textColor="@android:color/darker_gray"
android:textSize="10sp"/>
<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="10sp"/>
</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:gravity="center"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:textColor="@android:color/white"
android:textSize="11sp"/>
</LinearLayout>
</LinearLayout>
我做错了什么。大多数代码都直接从示例中复制