自動調整橫幅廣告是新一代的回應式廣告,可根據每種裝置調整廣告大小,盡可能提高成效。改善了智慧型橫幅廣告 (僅支援固定高度),自動調整橫幅廣告可讓開發人員指定廣告寬度,並據此決定最佳廣告大小。
為了選擇最合適的廣告大小,自動調整橫幅廣告採用固定的顯示比例,而非固定高度。如此一來,不同裝置螢幕顯示的橫幅廣告區塊就會更一致,並有機會提升成效。
請注意,使用自動調整橫幅廣告時,系統會針對特定裝置和寬度傳回固定的大小。在特定裝置上測試版面配置後,您可以確保廣告大小不會改變。不過,橫幅廣告素材的大小可能會在不同裝置上改變。因此,建議您確保版面配置能配合廣告高度變化。在極少數情況下,系統可能無法完整自動調整大小,而會改為將標準大小的廣告素材置於這個版位的中心。
自動調整橫幅廣告的使用時機
自動調整橫幅廣告的設計旨在直接取代業界標準的 320x50 橫幅廣告大小,以及疊加的智慧型橫幅廣告格式。
這些橫幅廣告大小常用為錨定橫幅廣告,通常會鎖定在螢幕的頂端或底部。對於這類錨定橫幅廣告,使用自動調整橫幅廣告時的顯示比例與標準 320x50 廣告相同,如以下螢幕截圖所示:
![]() 320x50 橫幅廣告 |
![]() 智慧型橫幅廣告 |
![]() 自動調整橫幅廣告 |
自動調整橫幅廣告能充分運用可用的螢幕大小。此外,與智慧型橫幅廣告相比,自動調整橫幅廣告是較適合的選擇,原因如下:
它會採用提供的寬度,而非全螢幕寬度,方便您考量 螢幕凹口。
它會針對特定裝置選取最佳高度,而不是在不同大小的裝置中使用固定高度,減少裝置分散的影響。
實作注意事項
在應用程式中實作自動調整橫幅廣告時,請注意以下幾點:
- 您必須知道要顯示廣告的檢視畫面寬度,且應將裝置寬度和任何螢幕凹口 納入考量。
當放送較小的廣告未填滿廣告版位時,請確保廣告檢視畫面背景不透明,符合AdMob 政策規定。
確認您使用的是最新版的 Google Mobile Ads SDK。 如果是中介服務,請使用最新版的中介服務轉接程式。
自動調整橫幅廣告大小在採用完整可用寬度時,會呈現最佳效果。在多數情況下,這會是使用中裝置螢幕的最大寬度。請務必考慮適用的螢幕凹口。
Google Mobile Ads SDK 會在
AdSize
中根據指定寬度傳回 的最佳化廣告高度。有三種方法可取得自動調整橫幅廣告的廣告大小,一種用於橫向,一種用於直向,另一種用於執行時目前的螢幕方向。詳情請參閱下方的完整的 API 說明文件。
在特定裝置上針對特定寬度傳回的尺寸一律會相同,因此在特定裝置上測試版面配置後,您可以確定廣告大小不會改變。
錨定橫幅廣告的高度絕不會超過裝置高度的 15%,也一律小於 50dp。
快速入門
導入簡易自動調整錨定橫幅廣告的步驟如下。
建立 an
AdView
物件並設定廣告單元 ID。取得自動調整橫幅廣告大小。您取得的尺寸會用於請求自動調整橫幅廣告。如要取得自動調整廣告大小,請務必確認以下事項:
- 取得目前使用的裝置的寬度。如果不想使用整個螢幕,則可自行設定寬度。
- 在廣告大小類別中使用適當的靜態方法,例如
AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(context, width)
即可取得所選方向的自動調整AdSize
物件。 - 您可以在橫幅廣告檢視畫面中設定廣告大小,使用
AdView.setAdSize()
進行操作。使用AdManagerAdView.setAdSizes()
的
以下提供完整示例。
建立廣告請求物件,並在準備好的廣告檢視畫面中使用
loadAd()
方法載入橫幅廣告,就跟使用一般橫幅廣告請求一樣。
程式碼範例
以下活動範例會載入配合螢幕寬度的自動調整橫幅廣告:
Java
import android.graphics.Rect; import android.os.Bundle; import android.widget.FrameLayout; import androidx.appcompat.app.AppCompatActivity import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdSize; import com.google.android.gms.ads.AdView; import com.google.android.gms.ads.MobileAds; import com.google.android.gms.ads.initialization.InitializationStatus; import com.google.android.gms.ads.initialization.OnInitializationCompleteListener; /** Main Activity. Inflates main activity xml and child fragments. */ public class MyActivity extends AppCompatActivity { private static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/6300978111"; private AdView adView; private FrameLayout adContainerView; private boolean initialLayoutComplete = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); // Initialize the Mobile Ads SDK. MobileAds.initialize(this, new OnInitializationCompleteListener() { @Override public void onInitializationComplete(InitializationStatus initializationStatus) { } }); adContainerView = findViewById(R.id.ad_view_container); adView = new AdView(this); adContainerView.addView(adView); // Since we're loading the banner based on the adContainerView size, we need // to wait until this view is laid out before we can get the width. adContainerView.getViewTreeObserver().addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { if (!initialLayoutComplete) { initialLayoutComplete = true; loadBanner(); } } }); } private void loadBanner() { adView.setAdUnitId(AD_UNIT_ID); AdSize adSize = getAdSize(); adView.setAdSize(adSize); // Create an ad request. Check your logcat output for the hashed device ID // to get test ads on a physical device, e.g., // "Use AdRequest.Builder.addTestDevice("ABCDE0123") to get test ads on this // device." AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR) .build(); // Start loading the ad in the background. adView.loadAd(adRequest); } // Determine the screen width (less decorations) to use for the ad width. private AdSize getAdSize() { WindowMetrics windowMetrics = getWindowManager().getCurrentWindowMetrics(); Rect bounds = windowMetrics.getBounds(); float adWidthPixels = adContainerView.getWidth(); // If the ad hasn't been laid out, default to the full screen width. if (adWidthPixels == 0f) { adWidthPixels = bounds.width(); } float density = getResources().getDisplayMetrics().density; int adWidth = (int) (adWidthPixels / density); return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth); } }
Kotlin
import android.os.Bundle import android.widget.FrameLayout import androidx.appcompat.app.AppCompatActivity import com.google.android.gms.ads.* /** Main Activity. Inflates main activity xml and child fragments. */ class MyActivity : AppCompatActivity() { private lateinit var adView: AdView private lateinit var adContainerView: FrameLayout private var initialLayoutComplete = false // Determine the screen width (less decorations) to use for the ad width. private val adSize: AdSize get() { val windowMetrics = windowManager.currentWindowMetrics val bounds = windowMetrics.bounds var adWidthPixels = adContainerView.width.toFloat() // If the ad hasn't been laid out, default to the full screen width. if (adWidthPixels == 0f) { adWidthPixels = bounds.width().toFloat() } val density = resources.displayMetrics.density val adWidth = (adWidthPixels / density).toInt() return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_my) // Initialize the Mobile Ads SDK. MobileAds.initialize(this) {} adContainerView = findViewById(R.id.ad_view_container) adView = AdView(this) adContainerView.addView(adView) // Since we're loading the banner based on the adContainerView size, we need // to wait until this view is laid out before we can get the width. adContainerView.viewTreeObserver.addOnGlobalLayoutListener { if (!initialLayoutComplete) { initialLayoutComplete = true loadBanner() } } } private fun loadBanner() { adView.adUnitId = AD_UNIT_ID adView.adSize(adSize) // Create an ad request. Check your logcat output for the hashed device ID to // get test ads on a physical device, e.g., // "Use AdRequest.Builder.addTestDevice("ABCDE0123") to get test ads on this device." val adRequest = AdRequest .Builder() .addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build() // Start loading the ad in the background. adView.loadAd(adRequest) } companion object { // This is an ad unit ID for a test ad. Replace with your own banner ad unit ID. private val AD_UNIT_ID = "ca-app-pub-3940256099942544/6300978111" } }
這裡 AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize
函式可用來取得在目前介面方向的錨定位置中的橫幅廣告大小。如要在特定方向預先載入錨定橫幅廣告,請使用 AdSize.getPortraitAnchoredAdaptiveBannerAdSize
和 AdSize.getLandscapeAnchoredAdaptiveBannerAdSize
的相關函式。
GitHub 上的完整範例
下載 Java | 下載 Kotlin |