当我尝试使用模板加载原生广告时,这是我使用的XML代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ededed">
<com.google.android.gms.ads.NativeExpressAdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="320x150"
ads:adUnitId="@string/native_ad_exit_app">
</com.google.android.gms.ads.NativeExpressAdView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/title_bold"
android:text="@string/sure_wanna_exit"
android:textColor="@android:color/white"
android:padding="@dimen/half_activity_horizontal_margin"
android:gravity="center"
android:background="@color/standard_android_blue"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/standard_button_size">
<Button android:id="@+id/btnStayApp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:text="@string/stay"
android:textColor="#999999"/>
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="@color/standard_android_blue"/>
<Button android:id="@+id/btnExitApp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:text="@string/exit"
android:textColor="@color/standard_android_blue"/>
</LinearLayout>
</LinearLayout>
这是java代码:
public class NativeAdDialog extends DialogFragment {
/**********************/
/** Create the dialog**/
/**********************/
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final View v = getActivity().getLayoutInflater().inflate(R.layout.native_ad_container, null);
/********************/
/** Exit app button */
/********************/
Button exitAppButton = (Button)v.findViewById(R.id.btnExitApp);
exitAppButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getActivity().finish();
}
});
/********************/
/** Stay app button */
/********************/
Button stayAppButton = (Button)v.findViewById(R.id.btnStayApp);
stayAppButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
/***********/
/** The ad */
/***********/
NativeExpressAdView adView = (NativeExpressAdView) v.findViewById(R.id.adView);
adView.loadAd(new AdRequest.Builder().build());
return new AlertDialog.Builder(getActivity()).setView(v).create();
}
}
失败后,我尝试在此项目中设置自定义原生广告: https://github.com/googleads/googleads-mobile-android-examples/tree/master/admob/NativeExample
所以我将下一个代码添加到我的java文件中:
/**
* Populates a {@link NativeAppInstallAdView} object with data from a given
* {@link NativeAppInstallAd}.
*
* @param nativeAppInstallAd the object containing the ad's assets
* @param adView the view to be populated
*/
private void populateAppInstallAdView(NativeAppInstallAd nativeAppInstallAd,
NativeAppInstallAdView adView) {
adView.setHeadlineView(adView.findViewById(R.id.appinstall_headline));
adView.setImageView(adView.findViewById(R.id.appinstall_image));
adView.setBodyView(adView.findViewById(R.id.appinstall_body));
adView.setCallToActionView(adView.findViewById(R.id.appinstall_call_to_action));
adView.setIconView(adView.findViewById(R.id.appinstall_app_icon));
adView.setPriceView(adView.findViewById(R.id.appinstall_price));
adView.setStarRatingView(adView.findViewById(R.id.appinstall_stars));
adView.setStoreView(adView.findViewById(R.id.appinstall_store));
// Some assets are guaranteed to be in every NativeAppInstallAd.
((TextView) adView.getHeadlineView()).setText(nativeAppInstallAd.getHeadline());
((TextView) adView.getBodyView()).setText(nativeAppInstallAd.getBody());
((Button) adView.getCallToActionView()).setText(nativeAppInstallAd.getCallToAction());
((ImageView) adView.getIconView()).setImageDrawable(nativeAppInstallAd.getIcon()
.getDrawable());
List<NativeAd.Image> images = nativeAppInstallAd.getImages();
if (images.size() > 0) {
((ImageView) adView.getImageView()).setImageDrawable(images.get(0).getDrawable());
}
// Some aren't guaranteed, however, and should be checked.
if (nativeAppInstallAd.getPrice() == null) {
adView.getPriceView().setVisibility(View.INVISIBLE);
} else {
adView.getPriceView().setVisibility(View.VISIBLE);
((TextView) adView.getPriceView()).setText(nativeAppInstallAd.getPrice());
}
if (nativeAppInstallAd.getStore() == null) {
adView.getStoreView().setVisibility(View.INVISIBLE);
} else {
adView.getStoreView().setVisibility(View.VISIBLE);
((TextView) adView.getStoreView()).setText(nativeAppInstallAd.getStore());
}
if (nativeAppInstallAd.getStarRating() == null) {
adView.getStarRatingView().setVisibility(View.INVISIBLE);
} else {
((RatingBar) adView.getStarRatingView())
.setRating(nativeAppInstallAd.getStarRating().floatValue());
adView.getStarRatingView().setVisibility(View.VISIBLE);
}
// Assign native ad object to the native view.
adView.setNativeAd(nativeAppInstallAd);
}
/**
* Populates a {@link NativeContentAdView} object with data from a given
* {@link NativeContentAd}.
*/
private void populateContentAdView(NativeContentAd nativeContentAd,
NativeContentAdView adView) {
adView.setHeadlineView(adView.findViewById(R.id.contentad_headline));
adView.setImageView(adView.findViewById(R.id.contentad_image));
adView.setBodyView(adView.findViewById(R.id.contentad_body));
adView.setCallToActionView(adView.findViewById(R.id.contentad_call_to_action));
adView.setLogoView(adView.findViewById(R.id.contentad_logo));
adView.setAdvertiserView(adView.findViewById(R.id.contentad_advertiser));
// Some assets are guaranteed to be in every NativeContentAd.
((TextView) adView.getHeadlineView()).setText(nativeContentAd.getHeadline());
((TextView) adView.getBodyView()).setText(nativeContentAd.getBody());
((TextView) adView.getCallToActionView()).setText(nativeContentAd.getCallToAction());
((TextView) adView.getAdvertiserView()).setText(nativeContentAd.getAdvertiser());
List<NativeAd.Image> images = nativeContentAd.getImages();
if (images.size() > 0) {
((ImageView) adView.getImageView()).setImageDrawable(images.get(0).getDrawable());
}
// Some aren't guaranteed, however, and should be checked.
NativeAd.Image logoImage = nativeContentAd.getLogo();
if (logoImage == null) {
adView.getLogoView().setVisibility(View.INVISIBLE);
} else {
((ImageView) adView.getLogoView()).setImageDrawable(logoImage.getDrawable());
adView.getLogoView().setVisibility(View.VISIBLE);
}
// Assign native ad object to the native view.
adView.setNativeAd(nativeContentAd);
}
/**
* Creates a request for a new native ad based on the boolean parameters and calls the
* corresponding "populate" method when one is successfully returned.
*/
private void refreshAd() {
AdLoader adLoader = new AdLoader.Builder(getActivity(), getResources().getString(R.string.native_ad_exit_app))
.forAppInstallAd(new NativeAppInstallAd.OnAppInstallAdLoadedListener() {
@Override
public void onAppInstallAdLoaded(NativeAppInstallAd appInstallAd) {
// Show the app install ad.
NativeAppInstallAdView adView = (NativeAppInstallAdView) getActivity().getLayoutInflater()
.inflate(R.layout.ad_app_install, null);
populateAppInstallAdView(appInstallAd, adView);
adPlaceholder.removeAllViews();
adPlaceholder.addView(adView);
}
})
.forContentAd(new NativeContentAd.OnContentAdLoadedListener() {
@Override
public void onContentAdLoaded(NativeContentAd contentAd) {
// Show the content ad.
NativeContentAdView adView = (NativeContentAdView) getActivity().getLayoutInflater()
.inflate(R.layout.ad_content, null);
populateContentAdView(contentAd, adView);
adPlaceholder.removeAllViews();
adPlaceholder.addView(adView);
}
})
.withAdListener(new AdListener() {
@Override
public void onAdFailedToLoad(int errorCode) {
// Handle the failure by logging, altering the UI, etc.
}
})
.withNativeAdOptions(new NativeAdOptions.Builder()
// Methods in the NativeAdOptions.Builder class can be
// used here to specify individual options settings.
.build())
.build();
adLoader.loadAd(new AdRequest.Builder().build());
}
我在onCreate方法上调用了refreshAd,但adLoader.isLoading()总是返回false。
我做错了什么?
答案 0 :(得分:2)
广告:adSize =“320x150”仅适用于small and medium template size native ads
更安全的测试方法是使用广告:adSize =“FULL_WIDTHx250”,它支持所有三种模板尺寸。 FULL_WIDTH不是强制性的,但我在这里使用它来向您展示可能性。您可以选择广告:adSize =“280x250”,而不是支持所有模板尺寸。
在为AdMob创建新的原生广告广告单元时,请确保选择正确的尺寸,因为将来无法修改此尺寸。允许AdMob几个小时开始向新创建的广告单元ID投放广告。
一旦您完成所有工作,您就可以更改广告的尺寸以最符合您的设计要求。