内嵌自适应横幅广告
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
采用自适应横幅广告时,您可以指定广告宽度,以确定最优的广告尺寸。自适应横幅广告还可针对每种设备优化广告尺寸,从而最大限度地提升广告效果。此方法有助于提升广告效果。
与锚定自适应横幅广告相比,内嵌自适应横幅广告更大、更高,并且使用的是可变高度而非固定高度。内嵌自适应横幅广告的高度可变,可能占据整个屏幕,也可能只达到您指定的高度上限。
您可以将内嵌自适应横幅广告放置在滚动显示的内容中,示例如下:

准备工作
在继续操作之前,请确保您已通读横幅广告入门指南。
植入自适应横幅广告
与锚定自适应横幅广告不同,内嵌自适应横幅广告使用内嵌自适应横幅广告尺寸进行加载。若要创建内嵌自适应广告尺寸,请完成以下操作:
获取所用设备的宽度,或者自行设置宽度(如果您不想使用屏幕的全宽)。
Kotlin
private val adWidth: Int
get() {
val displayMetrics = resources.displayMetrics
val adWidthPixels =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val windowMetrics: WindowMetrics = this.windowManager.currentWindowMetrics
windowMetrics.bounds.width()
} else {
displayMetrics.widthPixels
}
val density = displayMetrics.density
return (adWidthPixels / density).toInt()
}
Java
public int getAdWidth() {
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
int adWidthPixels = displayMetrics.widthPixels;
if (VERSION.SDK_INT >= VERSION_CODES.R) {
WindowMetrics windowMetrics = this.getWindowManager().getCurrentWindowMetrics();
adWidthPixels = windowMetrics.getBounds().width();
}
float density = displayMetrics.density;
return (int) (adWidthPixels / density);
}
使用广告尺寸类中的相应静态方法(例如 AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(Context context, int width)
),获取适用于所选屏幕方向的内嵌自适应广告尺寸对象。
Kotlin
val adView = AdView(this@MainActivity)
adView.setAdSize(AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(this, adWidth))
Java
final AdView adView = new AdView(MainActivity.this);
adView.setAdSize(AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(this, getAdWidth()));
在应用中植入自适应横幅广告时,请注意以下几点:
- 按照内嵌自适应横幅广告尺寸的设计,占满可用宽度时效果最佳。在大多数情况下,该尺寸指的是所用设备的屏幕全宽,或者横幅广告父级内容的全宽。您必须知道要放置广告的视图的宽度、设备宽度、父级内容宽度以及适用的安全区域。
根据屏幕方向调整内嵌自适应横幅广告尺寸
若要预加载特定屏幕方向的内嵌自适应横幅广告,请使用以下方法:
如果您的应用同时支持纵向视图和横向视图,并且您想预加载当前屏幕方向的自适应横幅广告,请使用 AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(Context context, int width)
。此方法会加载当前屏幕方向的广告。
限制内嵌自适应横幅广告的高度
默认情况下,如果内嵌自适应横幅广告在实例化时未指定 maxHeight
,其 maxHeight
值会等于设备高度。若要限制内嵌自适应横幅广告的高度,请使用 AdSize.getInlineAdaptiveBannerAdSize(int width, int maxHeight)
方法。
其他资源
GitHub 示例
下载示例应用,查看内嵌自适应横幅广告的实际效果。
Java
Kotlin
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-09-01。
[null,null,["最后更新时间 (UTC):2025-09-01。"],[[["\u003cp\u003eAdaptive banners optimize ad size for each device to maximize performance by letting you specify the ad width.\u003c/p\u003e\n"],["\u003cp\u003eInline adaptive banners, placed within scrolling content, are larger, taller, and use variable heights compared to anchored adaptive banners.\u003c/p\u003e\n"],["\u003cp\u003eYou can get an inline adaptive banner ad size using the \u003ccode\u003eAdSize\u003c/code\u003e class's static methods with device width or a custom width.\u003c/p\u003e\n"],["\u003cp\u003eFor specific orientation preloading or limiting banner height, utilize dedicated \u003ccode\u003eAdSize\u003c/code\u003e methods like \u003ccode\u003egetPortraitInlineAdaptiveBannerAdSize\u003c/code\u003e or \u003ccode\u003egetInlineAdaptiveBannerAdSize\u003c/code\u003e.\u003c/p\u003e\n"]]],["Inline adaptive banners, designed for scrolling content, allow specifying ad width for optimal sizing and performance. To implement, determine the device or desired ad width. Then, use methods like `AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize` or `AdSize.getInlineAdaptiveBannerAdSize` to obtain an ad size object. Finally, load an ad using `AdView` and `AdRequest`. You may limit the ad's height, and specific orientation methods are available to load the ad.\n"],null,["# Inline adaptive banners\n\nSelect platform: [Android](/admob/android/banner/inline-adaptive \"View this page for the Android platform docs.\") [iOS](/admob/ios/banner/inline-adaptive \"View this page for the iOS platform docs.\") [Flutter](/admob/flutter/banner/inline-adaptive \"View this page for the Flutter platform docs.\")\n\n\u003cbr /\u003e\n\nAdaptive banners let you specify the width of an ad to determine the optimal\nad size. Adaptive banners also maximize performance by optimizing the ad size\nfor each device. This approach results in opportunities for\nimproved performance.\n\nCompared to anchored adaptive banners, inline adaptive banners are larger,\ntaller, and use variable instead of fixed heights. Inline adaptive banners are\nof variable height, and might encompass the entire screen or a maximum height\nthat you specify.\n\nYou place inline adaptive banners in scrolling content, for example:\n\nBefore you begin\n----------------\n\nBefore continuing, make sure you have completed the getting started guide,\n[Banner ads](/admob/android/banner).\n\nImplement adaptive banners\n--------------------------\n\nUnlike anchored adaptive banners, inline adapter banners load using an inline\nadaptive banner size. To create an inline adaptive ad size, complete the\nfollowing:\n\n1. Get the width of the device in use, or set your own width if you don't want\n to use the full width of the screen.\n\n ### Kotlin\n\n private val adWidth: Int\n get() {\n val displayMetrics = resources.displayMetrics\n val adWidthPixels =\n if (Build.VERSION.SDK_INT \u003e= Build.VERSION_CODES.R) {\n val windowMetrics: WindowMetrics = this.windowManager.currentWindowMetrics\n windowMetrics.bounds.width()\n } else {\n displayMetrics.widthPixels\n }\n val density = displayMetrics.density\n return (adWidthPixels / density).toInt()\n } \n https://github.com/googleads/googleads-mobile-android-examples/blob/ccc290a583d7f552bdcf81ea76adc05beaa43f0b/kotlin/advanced/InlineAdaptiveBannerExample/app/src/main/java/com/google/android/gms/example/inlineadaptivebannerexample/MainActivity.kt#L70-L83\n\n ### Java\n\n public int getAdWidth() {\n DisplayMetrics displayMetrics = getResources().getDisplayMetrics();\n int adWidthPixels = displayMetrics.widthPixels;\n\n if (VERSION.SDK_INT \u003e= VERSION_CODES.R) {\n WindowMetrics windowMetrics = this.getWindowManager().getCurrentWindowMetrics();\n adWidthPixels = windowMetrics.getBounds().width();\n }\n\n float density = displayMetrics.density;\n return (int) (adWidthPixels / density);\n } \n https://github.com/googleads/googleads-mobile-android-examples/blob/ccc290a583d7f552bdcf81ea76adc05beaa43f0b/java/advanced/InlineAdaptiveBannerExample/app/src/main/java/com/google/android/gms/example/inlineadaptivebannerexample/MainActivity.java#L133-L145\n\n2. Use the appropriate static methods on the ad size class, such as\n\n `AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(Context context, int width)`\n\n to get an inline adaptive ad size object for the chosen orientation.\n\n ### Kotlin\n\n val adView = AdView(this@MainActivity)\n adView.setAdSize(AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(this, adWidth)) \n https://github.com/googleads/googleads-mobile-android-examples/blob/ccc290a583d7f552bdcf81ea76adc05beaa43f0b/kotlin/advanced/InlineAdaptiveBannerExample/app/src/main/java/com/google/android/gms/example/inlineadaptivebannerexample/MainActivity.kt#L120-L121\n\n ### Java\n\n final AdView adView = new AdView(MainActivity.this);\n adView.setAdSize(AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(this, getAdWidth())); \n https://github.com/googleads/googleads-mobile-android-examples/blob/ccc290a583d7f552bdcf81ea76adc05beaa43f0b/java/advanced/InlineAdaptiveBannerExample/app/src/main/java/com/google/android/gms/example/inlineadaptivebannerexample/MainActivity.java#L154-L155\n\n\u003cbr /\u003e\n\nWhen implementing adaptive banners in your app, note these points:\n\n- The inline adaptive banner sizes work best when using the full available width. In most cases, this size is the full width of the device screen in use, or the full width of the banner's parent content. You must know the width of the view to place in the ad, the device width, the parent content width, and applicable safe areas.\n\nOrient inline adaptive banner size\n----------------------------------\n\nTo preload an inline adaptive banner ad for a specific orientation, use the\nfollowing methods:\n\n- [`AdSize.getPortraitInlineAdaptiveBannerAdSize(Context context, int width)`](/admob/android/reference/com/google/android/gms/ads/AdSize#getPortraitInlineAdaptiveBannerAdSize(android.content.Context,int))\n\n- [`AdSize.getLandscapeInlineAdaptiveBannerAdSize(Context context, int width)`](/admob/android/reference/com/google/android/gms/ads/AdSize#getLandscapeInlineAdaptiveBannerAdSize(android.content.Context,int))\n\nIf your app supports both portrait and landscape views, and you want to preload\nan adaptive banner ad in the current orientation, use\n\n[`AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(Context context, int width)`](/admob/android/reference/com/google/android/gms/ads/AdSize#getCurrentOrientationInlineAdaptiveBannerAdSize(android.content.Context,int))\n\nThis method loads an ad in the current orientation.\n\n### Limit inline adaptive banner height\n\nBy default, inline adaptive banners instantiated without a `maxHeight` value\nhave a `maxHeight` equal to the device height. To limit the inline adaptive\nbanner height, use the\n\n[`AdSize.getInlineAdaptiveBannerAdSize(int width, int maxHeight)`](/admob/android/reference/com/google/android/gms/ads/AdSize#getInlineAdaptiveBannerAdSize(int,int))\n\nmethod.\n\nAdditional resources\n--------------------\n\n### Examples on GitHub\n\nDownload the sample application to see inline adaptive banners in action.\n\n[Java](//github.com/googleads/googleads-mobile-android-examples/tree/main/java/advanced/InlineAdaptiveBannerExample)\n[Kotlin](//github.com/googleads/googleads-mobile-android-examples/tree/main/kotlin/advanced/InlineAdaptiveBannerExample)\n\n\u003cbr /\u003e"]]