内嵌自适应横幅广告

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

为了选择最佳广告尺寸,内嵌自适应横幅广告会使用最大高度,而非固定高度。这有助于提升广告效果。

何时使用内嵌自适应横幅广告

与锚定自适应横幅广告相比,内嵌自适应横幅广告是一种更大、更高的横幅广告。这种广告的高度可变,与设备屏幕一样高。

它们应放置在滚动内容中,例如:

准备工作

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

  • 为使内嵌自适应横幅广告正常运行,您需要将布局设为自适应。 否则,可能会导致广告体验被剪裁或呈现不正确。
  • 您必须知道要在其中展示广告的视图的宽度,并且应在设置视图宽度时将设备宽度以及任何适用的安全区域考虑在内

  • 确保您使用的是最新版 Google 移动广告 SDK;如果您使用的是中介功能,请确保您使用的是最新版中介适配器。

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

  • 您可能需要更新或创建新订单项才能使用自适应尺寸。了解详情

实现

若要植入简单的内嵌自适应横幅广告,请按照以下步骤操作。

  1. 创建内嵌自适应横幅广告尺寸。您获取的尺寸将用于请求自适应横幅广告。要获取自适应广告尺寸,请务必执行以下操作:
    • 获取所用设备的宽度,或者自行设置宽度(如果您不想使用屏幕的全宽)。
    • 针对广告尺寸类使用相应的静态方法(例如 AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(Context context, int width) ),以获取所选屏幕方向的自适应 AdSize 对象。
    • 如果您想限制横幅广告的高度,可以使用静态方法 AdSize.getInlineAdaptiveBannerAdSize(int width, int maxHeight)
  2. 使用第 1 步中的广告尺寸创建 an AdManagerAdView 对象。此外,还请务必设置广告单元 ID 和根视图控制器。
    • 或者,对于现有 AdManagerAdView,请设置 使用 AdManagerAdView.setAdSize(AdSize adSize) 确定广告尺寸。
  3. 在预先准备的广告视图中,使用 loadRequest 方法创建广告请求对象并加载横幅广告,其处理方式与常规横幅广告请求一样。

以下示例代码演示了这些步骤:

Java

// Step 1: Create an inline adaptive banner ad size using the activity context.
AdSize adSize = AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(this, 320);

// Step 2: Create banner using activity context and set the inline ad size and
// ad unit ID.
AdManagerAdView bannerView = new AdManagerAdView(this);
bannerView.setAdUnitId("ad unit ID");

// Note that this sets both the adaptive ad size for backfill inventory as well
// as the supported reservation sizes.
bannerView.setAdSizes(adSize, AdSize.BANNER);

// Step 3: Load an ad.
AdManagerAdRequest adRequest = new AdManagerAdRequest.Builder().build();
bannerView.loadAd(adRequest);
// TODO: Insert banner view in list view or scroll view, etc.

Kotlin

// Step 1: Create an inline adaptive banner ad size using the activity context.
val adSize = AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(this, 320)

// Step 2: Create banner using activity context and set the inline ad size and
// ad unit ID.
val bannerView = AdManagerAdView(this)
bannerView.adUnitId = "ad unit ID"

// Note that this sets both the adaptive ad size for backfill inventory as well
// as the supported reservation sizes.
bannerView.setAdSizes(adSize, AdSize.BANNER)

// Step 3: Load an ad.
val adRequest = AdManagerAdRequest.Builder().build()
bannerView.loadAd(adRequest)
// TODO: Insert banner view in list view or scroll view, etc.

其他资源

GitHub 上的示例

下载示例应用,查看内嵌自适应横幅广告的实际效果。

Java Kotlin