نمایش NativeAd
وقتی یک تبلیغ بومی بارگیری میشود، Google Mobile Ads SDK شنونده را برای قالب آگهی مربوطه فراخوانی میکند. سپس برنامه شما مسئول نمایش آگهی است، اگرچه لزوماً نباید فوراً این کار را انجام دهد. برای آسانتر کردن نمایش قالبهای تبلیغاتی تعریفشده توسط سیستم، SDK منابع مفیدی را ارائه میدهد که در زیر توضیح داده شده است.
کلاس NativeAdView
را تعریف کنید
یک کلاس NativeAdView
تعریف کنید. این کلاس یک کلاس ViewGroup
است و ظرف سطح بالای کلاس NativeAdView
است. هر نمای تبلیغات بومی حاوی دارایی های تبلیغاتی بومی است، مانند عنصر نمای MediaView
یا عنصر نمای Title
، که باید فرزند شی NativeAdView
باشد.
طرح بندی XML
یک XML NativeAdView
به پروژه خود اضافه کنید:
<com.google.android.gms.ads.nativead.NativeAdView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical">
<LinearLayout
android:orientation="horizontal">
<ImageView
android:id="@+id/ad_app_icon" />
<TextView
android:id="@+id/ad_headline" />
</LinearLayout>
<!--Add remaining assets such as the image and media view.-->
</LinearLayout>
</com.google.android.gms.ads.nativead.NativeAdView>
Jetpack Compose
شامل ماژول JetpackComposeDemo/compose-util که شامل کمک کننده هایی برای نوشتن NativeAdView
و دارایی های آن است.
با استفاده از ماژول compose-util
، یک NativeAdView
بنویسید:
import com.google.android.gms.compose_util.NativeAdAttribution
import com.google.android.gms.compose_util.NativeAdView
@Composable
/** Display a native ad with a user defined template. */
fun DisplayNativeAdView(nativeAd: NativeAd) {
NativeAdView {
// Display the ad attribution.
NativeAdAttribution(text = context.getString("Ad"))
// Add remaining assets such as the image and media view.
}
}
aad بومی بارگذاری شده را مدیریت کنید
هنگامی که یک تبلیغ بومی بارگیری میشود، رویداد برگشت به تماس را مدیریت کنید، نمای تبلیغات بومی را افزایش دهید و آن را به سلسله مراتب مشاهده اضافه کنید:
جاوا
AdLoader.Builder builder = new AdLoader.Builder(this, "ca-app-pub-3940256099942544/2247696110")
.forNativeAd(new NativeAd.OnNativeAdLoadedListener() {
@Override
public void onNativeAdLoaded(NativeAd nativeAd) {
// Assumes you have a placeholder FrameLayout in your View layout
// (with ID fl_adplaceholder) where the ad is to be placed.
FrameLayout frameLayout =
findViewById(R.id.fl_adplaceholder);
// Assumes that your ad layout is in a file call native_ad_layout.xml
// in the res/layout folder
NativeAdView adView = (NativeAdView) getLayoutInflater()
.inflate(R.layout.native_ad_layout, null);
// This method sets the assets into the ad view.
populateNativeAdView(nativeAd, adView);
frameLayout.removeAllViews();
frameLayout.addView(adView);
}
});
کاتلین
val builder = AdLoader.Builder(this, "ca-app-pub-3940256099942544/2247696110")
.forNativeAd { nativeAd ->
// Assumes you have a placeholder FrameLayout in your View layout
// (with ID fl_adplaceholder) where the ad is to be placed.
val frameLayout: FrameLayout = findViewById(R.id.fl_adplaceholder)
// Assumes that your ad layout is in a file call native_ad_layout.xml
// in the res/layout folder
val adView = layoutInflater
.inflate(R.layout.native_ad_layout, null) as NativeAdView
// This method sets the assets into the ad view.
populateNativeAdView(nativeAd, adView)
frameLayout.removeAllViews()
frameLayout.addView(adView)
}
Jetpack Compose
@Composable
/** Load and display a native ad. */
fun NativeScreen() {
var nativeAd by remember { mutableStateOf<NativeAd?>(null) }
val context = LocalContext.current
var isDisposed by remember { mutableStateOf(false) }
DisposableEffect(Unit) {
// Load the native ad when we launch this screen
loadNativeAd(
context = context,
onAdLoaded = { ad ->
// Handle the native ad being loaded.
if (!isDisposed) {
nativeAd = ad
} else {
// Destroy the native ad if loaded after the screen is disposed.
ad.destroy()
}
},
)
// Destroy the native ad to prevent memory leaks when we dispose of this screen.
onDispose {
isDisposed = true
nativeAd?.destroy()
nativeAd = null
}
}
// Display the native ad view with a user defined template.
nativeAd?.let { adValue -> DisplayNativeAdView(adValue) }
}
fun loadNativeAd(context: Context, onAdLoaded: (NativeAd) -> Unit) {
val adLoader =
AdLoader.Builder(context, NATIVE_AD_UNIT_ID)
.forNativeAd { nativeAd -> onAdLoaded(nativeAd) }
.withAdListener(
object : AdListener() {
override fun onAdFailedToLoad(error: LoadAdError) {
Log.e(TAG, "Native ad failed to load: ${error.message}")
}
override fun onAdLoaded() {
Log.d(TAG, "Native ad was loaded.")
}
override fun onAdImpression() {
Log.d(TAG, "Native ad recorded an impression.")
}
override fun onAdClicked() {
Log.d(TAG, "Native ad was clicked.")
}
}
)
.build()
adLoader.loadAd(AdRequest.Builder().build())
}
توجه داشته باشید که همه داراییهای یک آگهی بومی معین باید در طرح NativeAdView
ارائه شوند. وقتی داراییهای بومی خارج از طرحبندی نمای تبلیغاتی بومی ارائه میشوند، Google Mobile Ads SDK سعی میکند هشداری را ثبت کند.
کلاسهای ad view همچنین روشهایی را ارائه میدهند که برای ثبت نمای مورد استفاده برای هر دارایی جداگانه و یکی برای ثبت خود شی NativeAd
استفاده میشوند. ثبت نماها به این روش به SDK اجازه می دهد تا به طور خودکار وظایفی مانند:
- ضبط کلیک ها
- ضبط برداشت زمانی که اولین پیکسل روی صفحه قابل مشاهده است
- نمایش همپوشانی AdChoices
همپوشانی AdChoices
یک پوشش AdChoices به هر نمای تبلیغاتی توسط SDK اضافه می شود. برای نشانواره AdChoices که بهطور خودکار درج میشود، در گوشه دلخواهتان در نمای تبلیغاتی بومیتان فضای خالی بگذارید. همچنین، مهم است که پوشش AdChoices به راحتی دیده شود، بنابراین رنگ های پس زمینه و تصاویر را به طور مناسب انتخاب کنید. برای اطلاعات بیشتر در مورد ظاهر و عملکرد روکش، به توضیحات فیلد تبلیغات بومی مراجعه کنید.
انتساب آگهی
شما باید یک انتساب آگهی را نمایش دهید تا نشان دهد که این نما یک تبلیغ است. در دستورالعمل های خط مشی ما بیشتر بیاموزید.
نمونه کد
این مراحل برای نمایش یک آگهی بومی است:
- یک نمونه از کلاس
NativeAdView
ایجاد کنید. برای هر دارایی تبلیغاتی که باید نمایش داده شود:
- نمای دارایی را با دارایی موجود در شیء تبلیغ پر کنید.
- نمای دارایی را با کلاس
NativeAdView
ثبت کنید.
اگر طرحبندی تبلیغات بومی شما دارای دارایی رسانه بزرگ است،
MediaView
را ثبت کنید.شیء تبلیغ را با کلاس
NativeAdView
ثبت کنید.
در اینجا یک تابع مثال است که NativeAd
نمایش می دهد:
جاوا
private void displayNativeAd(ViewGroup parent, NativeAd ad) {
// Inflate a layout and add it to the parent ViewGroup.
LayoutInflater inflater = (LayoutInflater) parent.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
NativeAdView adView = (NativeAdView) inflater
.inflate(R.layout.ad_layout_file, parent);
// Locate the view that will hold the headline, set its text, and call the
// NativeAdView's setHeadlineView method to register it.
TextView headlineView = adView.findViewById<TextView>(R.id.ad_headline);
headlineView.setText(ad.getHeadline());
adView.setHeadlineView(headlineView);
// Repeat the process for the other assets in the NativeAd
// using additional view objects (Buttons, ImageViews, etc).
// If the app is using a MediaView, it should be
// instantiated and passed to setMediaView. This view is a little different
// in that the asset is populated automatically, so there's one less step.
MediaView mediaView = (MediaView) adView.findViewById(R.id.ad_media);
adView.setMediaView(mediaView);
// Call the NativeAdView's setNativeAd method to register the
// NativeAdObject.
adView.setNativeAd(ad);
// Ensure that the parent view doesn't already contain an ad view.
parent.removeAllViews();
// Place the AdView into the parent.
parent.addView(adView);
}
کاتلین
fun displayNativeAd(parent: ViewGroup, ad: NativeAd) {
// Inflate a layout and add it to the parent ViewGroup.
val inflater = parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)
as LayoutInflater
val adView = inflater.inflate(R.layout.ad_layout_file, parent) as NativeAdView
// Locate the view that will hold the headline, set its text, and use the
// NativeAdView's headlineView property to register it.
val headlineView = adView.findViewById<TextView>(R.id.ad_headline)
headlineView.text = ad.headline
adView.headlineView = headlineView
// Repeat the process for the other assets in the NativeAd using
// additional view objects (Buttons, ImageViews, etc).
val mediaView = adView.findViewById<MediaView>(R.id.ad_media)
adView.mediaView = mediaView
// Call the NativeAdView's setNativeAd method to register the
// NativeAdObject.
adView.setNativeAd(ad)
// Ensure that the parent view doesn't already contain an ad view.
parent.removeAllViews()
// Place the AdView into the parent.
parent.addView(adView)
}
Jetpack Compose
@Composable
/** Display a native ad with a user defined template. */
fun DisplayNativeAdView(nativeAd: NativeAd) {
val context = LocalContext.current
Box(modifier = Modifier.padding(8.dp).wrapContentHeight(Alignment.Top)) {
// Call the NativeAdView composable to display the native ad.
NativeAdView {
// Inside the NativeAdView composable, display the native ad assets.
Column(Modifier.align(Alignment.TopStart).wrapContentHeight(Alignment.Top)) {
// Display the ad attribution.
NativeAdAttribution(text = context.getString(R.string.attribution))
Row {
// If available, display the icon asset.
nativeAd.icon?.let { icon ->
NativeAdIconView(Modifier.padding(5.dp)) {
icon.drawable?.toBitmap()?.let { bitmap ->
Image(bitmap = bitmap.asImageBitmap(), "Icon")
}
}
}
Column {
// If available, display the headline asset.
nativeAd.headline?.let {
NativeAdHeadlineView {
Text(text = it, style = MaterialTheme.typography.headlineLarge)
}
}
// If available, display the star rating asset.
nativeAd.starRating?.let {
NativeAdStarRatingView {
Text(text = "Rated $it", style = MaterialTheme.typography.labelMedium)
}
}
}
}
// If available, display the body asset.
nativeAd.body?.let { NativeAdBodyView { Text(text = it) } }
// Display the media asset.
NativeAdMediaView(Modifier.fillMaxWidth().height(500.dp).fillMaxHeight())
Row(Modifier.align(Alignment.End).padding(5.dp)) {
// If available, display the price asset.
nativeAd.price?.let {
NativeAdPriceView(Modifier.padding(5.dp).align(Alignment.CenterVertically)) {
Text(text = it)
}
}
// If available, display the store asset.
nativeAd.store?.let {
NativeAdStoreView(Modifier.padding(5.dp).align(Alignment.CenterVertically)) {
Text(text = it)
}
}
// If available, display the call to action asset.
// Note: The Jetpack Compose button implements a click handler which overrides the native
// ad click handler, causing issues. Use the NativeAdButton which does not implement a
// click handler. To handle native ad clicks, use the NativeAd AdListener onAdClicked
// callback.
nativeAd.callToAction?.let { callToAction ->
NativeAdCallToActionView(Modifier.padding(5.dp)) { NativeAdButton(text = callToAction) }
}
}
}
}
}
}
در اینجا وظایف فردی وجود دارد:
طرح را باد کنید
جاوا
LayoutInflater inflater = (LayoutInflater) parent.getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); NativeAdView adView = (NativeAdView) inflater .inflate(R.layout.ad_layout_file, parent);
کاتلین
val inflater = parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater val adView = inflater.inflate(R.layout.ad_layout_file, parent) as NativeAdView
این کد یک طرح بندی XML را که حاوی نماهایی برای نمایش یک آگهی بومی است و سپس مکان مرجعی به
NativeAdView
را افزایش می دهد. توجه داشته باشید که اگرNativeAdView
موجود در قطعه یا فعالیت شما وجود دارد، میتوانید مجدداً از NativeAdView استفاده کنید، یا حتی یک نمونه را به صورت پویا بدون استفاده از فایل طرحبندی ایجاد کنید.نماهای دارایی را پر و ثبت کنید
این کد نمونه، نمای مورد استفاده برای نمایش عنوان را تعیین می کند، متن آن را با استفاده از دارایی رشته ارائه شده توسط شی آگهی تنظیم می کند و آن را با شی
NativeAdView
ثبت می کند:جاوا
TextView headlineView = adView.findViewById<TextView>(R.id.ad_headline); headlineView.setText(ad.getHeadline()); adView.setHeadlineView(headlineView);
کاتلین
val headlineView = adView.findViewById<TextView>(R.id.ad_headline) headlineView.text = ad.headline adView.headlineView = headlineView
این فرآیند مکان یابی نما، تنظیم مقدار آن، و ثبت آن در کلاس نمای تبلیغاتی باید برای هر یک از دارایی های ارائه شده توسط شی تبلیغاتی بومی که برنامه نمایش می دهد تکرار شود.
رسیدگی به کلیک ها
هیچ کنترل کننده کلیک سفارشی را روی هیچ نمایی در داخل یا در نمای تبلیغاتی بومی اجرا نکنید. کلیکها روی داراییهای مشاهده آگهی توسط SDK انجام میشود تا زمانی که بازدیدهای دارایی را به درستی پر کرده و ثبت کنید، همانطور که در بخش قبل بحث شد.
برای گوش دادن به کلیکها، پاسخ تماس کلیکی Google Mobile Ads SDK را اجرا کنید:
جاوا
AdLoader adLoader = new AdLoader.Builder(context, "ca-app-pub-3940256099942544/2247696110") // ... .withAdListener(new AdListener() { @Override public void onAdFailedToLoad(LoadAdError adError) { // Handle the failure by logging. } @Override public void onAdClicked() { // Log the click event or other custom behavior. } }) .build();
کاتلین
val adLoader = AdLoader.Builder(this, "ca-app-pub-3940256099942544/2247696110") // ... .withAdListener(object : AdListener() { override fun onAdFailedToLoad(adError: LoadAdError) { // Handle the failure. } override fun onAdClicked() { // Log the click event or other custom behavior. } }) .build()
MediaView را ثبت کنید
اگر میخواهید یک دارایی تصویر اصلی را در طرحبندی آگهی بومی خود بگنجانید، باید از دارایی
MediaView
به جای داراییImageView
استفاده کنید.MediaView
یکView
ویژه است که برای نمایش دارایی رسانه اصلی، ویدیو یا تصویر طراحی شده است.MediaView
می توان در یک طرح XML تعریف کرد یا به صورت پویا ساخته شد. باید در سلسله مراتب view یکNativeAdView
قرار گیرد، درست مانند هر نمای دارایی دیگر. برنامه هایی که ازMediaView
استفاده می کنند باید آن را درNativeAdView
ثبت کنند:جاوا
// Populate and register the media asset view. nativeAdView.setMediaView(nativeAdBinding.adMedia);
کاتلین
// Populate and register the media asset view. nativeAdView.mediaView = nativeAdBinding.adMedia
ImageScaleType
کلاس
MediaView
دارای ویژگیImageScaleType
هنگام نمایش تصاویر است. اگر می خواهید نحوه مقیاس بندی یک تصویر را درMediaView
تغییر دهید،ImageView.ScaleType
مربوطه را با استفاده از متدsetImageScaleType()
MediaView
تنظیم کنید:جاوا
mediaView.setImageScaleType(ImageView.ScaleType.CENTER_CROP);
کاتلین
mediaView.imageScaleType = ImageView.ScaleType.CENTER_CROP
MediaContent
کلاس
MediaContent
داده های مربوط به محتوای رسانه ای تبلیغ بومی را نگه می دارد که با استفاده از کلاسMediaView
نمایش داده می شود. هنگامی که ویژگیMediaView
mediaContent
با یک نمونهMediaContent
تنظیم می شود:اگر دارایی ویدیویی در دسترس باشد، بافر شده و در
MediaView
شروع به پخش میکند. با بررسیhasVideoContent()
میتوانید متوجه شوید که یک دارایی ویدیو در دسترس است.اگر تبلیغ حاوی دارایی ویدیو نباشد، دارایی
mainImage
دانلود شده و در داخلMediaView
قرار می گیرد.
به طور پیش فرض،
mainImage
اولین دارایی تصویر دانلود شده است. اگرsetReturnUrlsForImageAssets(true)
استفاده شود،mainImage
null
است و باید ویژگیmainImage
را روی تصویر دانلود شده دستی خود تنظیم کنید. توجه داشته باشید که این تصویر تنها زمانی استفاده خواهد شد که هیچ دارایی ویدیویی در دسترس نباشد.شیء تبلیغاتی بومی را ثبت کنید
این مرحله نهایی شیء تبلیغاتی بومی را با نمای مسئول نمایش آن ثبت می کند.
جاوا
adView.setNativeAd(ad);
کاتلین
adView.setNativeAd(ad)
تبلیغ را نابود کنید
پس از اتمام نمایش آگهی بومی خود، باید آن را از بین ببرید تا آگهی به درستی زباله جمع آوری شود.
جاوا
nativeAd.destroy();
کاتلین
nativeAd.destroy()
نمونه هایی در GitHub
مثال اجرای کامل تبلیغات بومی:
مراحل بعدی
موضوعات زیر را بررسی کنید: