顯示 NativeAd
原生廣告載入時,Google Mobile Ads SDK 會為對應的廣告格式叫用事件監聽器。接著,您的應用程式負責顯示廣告,但不一定需要立即顯示。為讓您更輕鬆地顯示系統定義的廣告格式,SDK 提供一些實用資源,如下所述。
NativeAdView
類別
NativeAd
格式有對應的 NativeAdView
類別。這個類別是發布商應用於 NativeAd
的根目錄 ViewGroup
。單一 NativeAdView
對應至單一原生廣告。用於顯示廣告素材資源的每個檢視畫面 (例如顯示螢幕截圖素材資源的 ImageView
) 都應為 NativeAdView
物件的子項。
原生廣告使用 LinearLayout
顯示素材資源檢視區塊的檢視區塊階層可能如下所示:
<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>
以下範例說明如何建立 NativeAdView
,並使用 NativeAd
填入資料:
Java
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);
}
});
Kotlin
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)
}
請注意,特定原生廣告的所有素材資源都應在 NativeAdView
版面配置中算繪。當原生素材資源在原生廣告視圖版面配置外顯示時,Google Mobile Ads SDK 會嘗試記錄警告。
廣告檢視畫面類別也提供方法,用於註冊用於個別素材資源的檢視畫面,以及註冊 NativeAd
物件本身。以這種方式註冊檢視畫面可讓 SDK 自動處理下列工作:
- 記錄點擊
- 在畫面上顯示第一個像素時,記錄曝光
- 顯示 AdChoices 重疊廣告
AdChoices 重疊廣告
SDK 會在每個廣告檢視畫面中加入 AdChoices 重疊圖示。在原生廣告檢視畫面的偏好角落保留空間,以便自動插入 AdChoices 標誌。此外,疊加在廣告中的 AdChoices 標籤必須清楚易見,因此請選用合適的背景顏色和圖片。如要進一步瞭解疊加層的外觀和功能,請參閱原生廣告欄位說明。
廣告標示
您必須顯示廣告標示,指出觀看次數是廣告。如要進一步瞭解相關政策,請參閱政策指南。
程式碼範例
以下是顯示原生廣告的步驟:
- 建立
NativeAdView
類別的例項。 針對每項要顯示的廣告素材資源:
- 使用廣告物件中的素材資源,填入素材資源檢視畫面。
- 使用
NativeAdView
類別註冊素材資源檢視畫面。
如果原生廣告版面配置包含大型媒體素材資源,請註冊
MediaView
。使用
NativeAdView
類別註冊廣告物件。
以下是顯示 NativeAd
的範例函式:
Java
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);
}
Kotlin
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)
}
以下是個別工作:
加載版面配置
Java
LayoutInflater inflater = (LayoutInflater) parent.getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); NativeAdView adView = (NativeAdView) inflater .inflate(R.layout.ad_layout_file, parent);
Kotlin
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
物件:Java
TextView headlineView = adView.findViewById<TextView>(R.id.ad_headline); headlineView.setText(ad.getHeadline()); adView.setHeadlineView(headlineView);
Kotlin
val headlineView = adView.findViewById<TextView>(R.id.ad_headline) headlineView.text = ad.headline adView.headlineView = headlineView
針對應用程式將要顯示的原生廣告物件提供的每個素材資源,都應重複執行這個尋找檢視畫面、設定其值,以及向廣告檢視畫面類別註冊的程序。
處理點擊
請勿在原生廣告檢視畫面內或上方的任何檢視畫面中,實作任何自訂點擊處理常式。只要您正確填入並註冊素材資源檢視畫面 (如前一個部分所述),SDK 就會處理廣告檢視畫面素材資源的點擊次數。
如要接收點擊事件,請實作 Google Mobile Ads SDK 點擊回呼:
Java
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();
Kotlin
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 版面配置中定義,也可以動態建構。它應放置在NativeAdView
的檢視區塊階層中,就像任何其他資產檢視畫面一樣。使用MediaView
的應用程式必須向NativeAdView
註冊:Java
// Populate and register the media asset view. nativeAdView.setMediaView(nativeAdBinding.adMedia);
Kotlin
// Populate and register the media asset view. nativeAdView.mediaView = nativeAdBinding.adMedia
ImageScaleType
MediaView
類別在顯示圖片時具有ImageScaleType
屬性。如要變更圖片在MediaView
中的縮放方式,請使用MediaView
的setImageScaleType()
方法設定對應的ImageView.ScaleType
:Java
mediaView.setImageScaleType(ImageView.ScaleType.CENTER_CROP);
Kotlin
mediaView.imageScaleType = ImageView.ScaleType.CENTER_CROP
MediaContent
MediaContent
類別會保留與原生廣告媒體內容相關的資料,並使用MediaView
類別顯示。當MediaView
mediaContent
屬性設為MediaContent
例項時:如果有可用的影片素材資源,系統會進行緩衝,並開始在
MediaView
中播放。您可以檢查hasVideoContent()
,判斷影片素材資源是否可用。如果廣告不含影片素材資源,系統會下載
mainImage
素材資源,並將其放入MediaView
中。
根據預設,
mainImage
是第一個下載的圖片素材資源。如果使用setReturnUrlsForImageAssets(true)
,mainImage
就是null
,您必須將mainImage
屬性設為手動下載的圖片。請注意,只有在沒有可用的影片素材資源時,系統才會使用這張圖片。註冊原生廣告物件
這個最後步驟會將原生廣告物件註冊至負責顯示廣告的檢視畫面。
Java
adView.setNativeAd(ad);
Kotlin
adView.setNativeAd(ad)
銷毀廣告
停止顯示原生廣告後,請務必將其銷毀,以便系統正確進行垃圾收集。
Java
nativeAd.destroy();
Kotlin
nativeAd.destroy()
GitHub 上的範例
完整的原生廣告實作範例:
後續步驟
請參閱下列主題: