锚定自适应横幅广告

自适应横幅广告是新一代自适应广告,可针对每台设备优化广告尺寸,从而最大限度地提升广告效果。自适应横幅广告基于仅支持固定高度的智能横幅广告进行了完善,可让开发者指定广告宽度,进而据此确定最佳广告尺寸。

为了选择最佳广告尺寸,自适应横幅广告采用的是固定宽高比,而非固定高度。因此,这种横幅广告在不同设备上占据的屏幕区域更为一致,也带来了提升广告效果的机会。

请注意,使用自适应横幅广告时,如果给定设备类型和宽度,则返回的广告尺寸将始终固定不变。在给定设备上测试布局后,便可以确定广告尺寸不会发生变化。不过,横幅广告素材的尺寸在不同设备上可能会有所不同。因此,建议您确保广告布局可以适应各种广告高度。 在极少数情况下,广告素材可能填不满整个自适应广告单元的尺寸,此时会改而展示一个标准尺寸的广告素材,并在广告位中居中显示它。

自适应横幅广告适用情形

自适应横幅广告可直接替换行业标准的 320x50 横幅广告尺寸及自适应横幅广告格式。

这些横幅广告尺寸通常用于锚定横幅广告,锚定横幅广告通常固定在屏幕的顶部或底部展示。对于此类锚定横幅广告,当使用自适应横幅广告时,宽高比与标准 320x50 广告大致相当,如以下三张屏幕截图所示:


320x50 横幅广告

智能横幅广告

自适应横幅广告

自适应横幅广告可更好地利用可用的屏幕尺寸。此外,与智能横幅广告相比,自适应横幅广告是一种更好的选择,原因如下:

  • 这种广告采用提供的宽度(而不是全屏宽度),这样您就可以将 刘海屏考虑在内。

  • 它会针对具体设备选择最优高度,而不是针对不同尺寸的设备都使用一个固定的高度,从而降低了设备屏幕尺寸多样造成的影响。

实现说明

在应用中植入自适应横幅广告时,请注意以下几点:

  • 您必须知道要展示广告的视图的宽度,并且在确定视图宽度时应考虑到设备宽度,以及任何适用的刘海屏
  • 当较小尺寸的广告不能填充广告位时,请确保您的广告视图背景是不透明的,以符合AdMob 政策。

  • 确保您使用的是最新版 Google 移动广告 SDK。 对于中介,请使用最新版中介适配器。

  • 自适应横幅广告尺寸经过专门设计,占满可用宽度时效果最佳。在大多数情况下,这里指的是所用设备的屏幕全宽。请务必考虑适用的刘海屏。

  • Google 移动广告 SDK 在 AdSize 中针对指定的宽度返回经过优化的广告高度。 。

  • 获取自适应横幅广告尺寸的方法有三种:一种适用于横向屏幕,一种适用于纵向屏幕,还有一个适用于执行操作时的屏幕方向。如需了解详情,请参阅下面的完整 API 文档。

  • 在给定设备上针对给定宽度返回的广告尺寸始终相同,因此在给定设备上测试布局后,您可以确定广告尺寸不会发生变化。

  • 锚定横幅广告的高度始终不会超过设备高度的 15%,也始终不小于 50dp。

快速入门

要植入简单的自适应锚定横幅广告,请按以下步骤操作。

  1. 创建 an AdView 对象并设置广告单元 ID。

  2. 获取自适应横幅广告尺寸。您获取的尺寸将用于请求自适应横幅广告。要获取自适应广告尺寸,请务必执行以下操作:

    1. 获取所用设备的宽度,或者自行设置宽度(如果您不想使用屏幕的全宽)。
    2. 针对广告尺寸类使用相应的静态方法(例如 AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(context, width) ),以获取所选屏幕方向的自适应AdSize 对象。
    3. 在横幅广告视图中设置广告尺寸,就是使用 AdView.setAdSize() 执行此操作。

    下面列出了完整示例。

  3. 在预先准备的广告视图中,使用loadAd()方法创建广告请求对象并加载横幅广告,其处理方式与常规横幅广告请求一样。

示例代码

下面是一个将加载自适应横幅广告以适应屏幕宽度的 Activity 示例:

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.getPortraitAnchoredAdaptiveBannerAdSizeAdSize.getLandscapeAnchoredAdaptiveBannerAdSize 中的相关函数。

GitHub 上的完整示例

下载 Java 下载 Kotlin