I banner adattivi rappresentano la nuova generazione di annunci adattabili, in grado di massimizzare il rendimento grazie alla capacità di adattare le dimensioni dell'annuncio per qualsiasi dispositivo. Garantendo un ulteriore salto di qualità rispetto ai banner
di dimensioni fisse, che supportavano solo altezze fisse, i banner adattivi consentono agli sviluppatori
di specificare la larghezza dell'annuncio e di utilizzarla per determinare le dimensioni ottimali dell'annuncio.
Per scegliere la dimensione migliore, i banner adattivi in linea utilizzano altezze massime anziché fisse. Ciò si traduce in opportunità per migliorare il rendimento.
Quando utilizzare i banner adattivi in linea
I banner adattivi in linea sono più grandi e più alti rispetto ai banner adattivi ancorati. Hanno un'altezza variabile e possono occupare l'intero schermo del dispositivo.
Sono pensati per essere inseriti nei contenuti scorrevoli, ad esempio:
Quando implementi i banner adattivi nella tua app, tieni presente quanto segue:
Assicurati di utilizzare la versione più recente dell'SDK Google Mobile Ads e, se utilizzi la mediazione, le versioni più recenti degli adattatori di mediazione.
Le dimensioni dei banner adattivi in linea sono progettate per funzionare al meglio quando viene utilizzata l'intera larghezza disponibile. Nella maggior parte dei casi, corrisponde alla larghezza massima dello schermo del dispositivo in uso. Assicurati di tenere conto delle aree di sicurezza applicabili.
I metodi per ottenere le dimensioni dell'annuncio sono
AdSize.getInlineAdaptiveBannerAdSize(int width, int maxHeight)
Quando utilizzi le API per banner adattivi in linea, l'SDK Google Mobile Ads
restituisce un AdSize con la larghezza specificata e un
flag in linea. L'altezza è zero o maxHeight, a seconda dell'API che utilizzi. L'altezza effettiva dell'annuncio viene resa disponibile quando viene restituita.
Un banner adattivo in linea è progettato per essere posizionato in contenuti scorrevoli. Il
banner può essere alto quanto lo schermo del dispositivo o limitato da un'altezza massima,
a seconda dell'API.
Implementazione
Segui i passaggi riportati di seguito per implementare un semplice banner adattivo in linea.
Ottieni le dimensioni di un annuncio banner adattivo in linea. La dimensione ottenuta verrà utilizzata per
richiedere il banner adattivo. Per ottenere le dimensioni adattive dell'annuncio, assicurati di:
Ottieni la larghezza del dispositivo in uso in pixel indipendenti dalla densità o imposta
la tua larghezza se non vuoi utilizzare la larghezza massima dello schermo.
Puoi utilizzare MediaQuery.of(context) per ottenere la larghezza dello schermo.
Utilizza i metodi statici appropriati nella classe delle dimensioni dell'annuncio, ad esempio
AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(int width)
per ottenere un oggetto AdSize adattivo in linea per l'orientamento corrente.
Se vuoi limitare l'altezza del banner, puoi utilizzare il metodo statico
AdSize.getInlineAdaptiveBannerAdSize(int width, int maxHeight).
Crea un oggetto BannerAd con l'ID unità pubblicitaria, le dimensioni dell'annuncio adattivo e
un oggetto richiesta annuncio.
Carica l'annuncio.
Nel callback onAdLoaded, utilizza BannerAd.getPlatformAdSize() per ottenere
le dimensioni aggiornate dell'annuncio sulla piattaforma e aggiorna l'altezza del contenitore AdWidget.
Codice di esempio
Ecco un esempio di widget che carica un banner adattivo in linea per adattarsi alla
larghezza dello schermo, tenendo conto degli inset:
import'package:flutter/material.dart';import'package:google_mobile_ads/google_mobile_ads.dart';/// This example demonstrates inline adaptive banner ads.////// Loads and shows an inline adaptive banner ad in a scrolling view,/// and reloads the ad when the orientation changes.classInlineAdaptiveExampleextendsStatefulWidget{@override_InlineAdaptiveExampleStatecreateState()=>_InlineAdaptiveExampleState();}class_InlineAdaptiveExampleStateextendsState<InlineAdaptiveExample>{staticconst_insets=16.0;BannerAd?_inlineAdaptiveAd;bool_isLoaded=false;AdSize?_adSize;lateOrientation_currentOrientation;doubleget_adWidth=>MediaQuery.of(context).size.width-(2*_insets);@overridevoiddidChangeDependencies(){super.didChangeDependencies();_currentOrientation=MediaQuery.of(context).orientation;_loadAd();}void_loadAd()async{await_inlineAdaptiveAd?.dispose();setState((){_inlineAdaptiveAd=null;_isLoaded=false;});// Get an inline adaptive size for the current orientation.AdSizesize=AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(_adWidth.truncate());_inlineAdaptiveAd=BannerAd(// TODO: replace this test ad unit with your own ad unit.adUnitId:'ca-app-pub-3940256099942544/9214589741',size:size,request:AdRequest(),listener:BannerAdListener(onAdLoaded:(Adad)async{print('Inline adaptive banner loaded: ${ad.responseInfo}');// After the ad is loaded, get the platform ad size and use it to// update the height of the container. This is necessary because the// height can change after the ad is loaded.BannerAdbannerAd=(adasBannerAd);finalAdSize?size=awaitbannerAd.getPlatformAdSize();if(size==null){print('Error: getPlatformAdSize() returned null for $bannerAd');return;}setState((){_inlineAdaptiveAd=bannerAd;_isLoaded=true;_adSize=size;});},onAdFailedToLoad:(Adad,LoadAdErrorerror){print('Inline adaptive banner failedToLoad: $error');ad.dispose();},),);await_inlineAdaptiveAd!.load();}/// Gets a widget containing the ad, if one is loaded.////// Returns an empty container if no ad is loaded, or the orientation/// has changed. Also loads a new ad if the orientation changes.Widget_getAdWidget(){returnOrientationBuilder(builder:(context,orientation){if(_currentOrientation==orientation&&
_inlineAdaptiveAd!=null&&
_isLoaded&&
_adSize!=null){returnAlign(child:Container(width:_adWidth,height:_adSize!.height.toDouble(),child:AdWidget(ad:_inlineAdaptiveAd!,),));}// Reload the ad if the orientation changes.if(_currentOrientation!=orientation){_currentOrientation=orientation;_loadAd();}returnContainer();},);}@overrideWidgetbuild(BuildContextcontext)=>Scaffold(appBar:AppBar(title:Text('Inline adaptive banner example'),),body:Center(child:Padding(padding:constEdgeInsets.symmetric(horizontal:_insets),child:ListView.separated(itemCount:20,separatorBuilder:(BuildContextcontext,intindex){returnContainer(height:40,);},itemBuilder:(BuildContextcontext,intindex){if(index==10){return_getAdWidget();}returnText('Placeholder text',style:TextStyle(fontSize:24),);},),),));@overridevoiddispose(){super.dispose();_inlineAdaptiveAd?.dispose();}}
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Mancano le informazioni di cui ho bisogno","missingTheInformationINeed","thumb-down"],["Troppo complicato/troppi passaggi","tooComplicatedTooManySteps","thumb-down"],["Obsoleti","outOfDate","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Problema relativo a esempi/codice","samplesCodeIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-09-06 UTC."],[[["\u003cp\u003eAdaptive banners optimize ad size for each device, maximizing performance by using the provided ad width to determine the optimal size, unlike fixed-size banners with fixed heights.\u003c/p\u003e\n"],["\u003cp\u003eInline adaptive banners are variable-height banners, potentially as tall as the device screen, designed for placement within scrolling content.\u003c/p\u003e\n"],["\u003cp\u003eImplementation requires using the latest Google Mobile Ads SDK, providing the ad width (considering safe areas), and using the \u003ccode\u003eAdSize\u003c/code\u003e methods to get the adaptive banner size.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers need to update the ad container height after the ad loads using \u003ccode\u003eBannerAd.getPlatformAdSize()\u003c/code\u003e to accommodate potential height changes.\u003c/p\u003e\n"]]],[],null,["Select 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 are the next generation of responsive ads, maximizing\nperformance by optimizing ad size for each device. Improving on fixed-size\nbanners, which only supported fixed heights, adaptive banners let developers\nspecify the ad-width and use this to determine the optimal ad size.\n\nTo pick the best ad size, inline adaptive banners use maximum instead of fixed\nheights. This results in opportunities for improved performance.\n\nWhen to use inline adaptive banners\n\nInline adaptive banners are larger, taller banners compared to anchored adaptive\nbanners. They are of variable height, and can be as tall as the device screen.\n\nThey are intended to be placed in scrolling content, for example:\n\nPrerequisites\n\n- Follow the instructions from the [Get Started guide](/admob/flutter/quick-start) on how to [Import the Mobile Ads Flutter plugin](/admob/flutter/quick-start#import_the_mobile_ads_sdk).\n\nBefore you begin **Important:** You must know the width of the view that the ad will be placed in, **and this should take into account the device width and any safe areas that are\n| applicable**.\n\nWhen implementing adaptive banners in your app, note these points:\n\n- Ensure you are using the latest version of Google Mobile Ads SDK, and if\n using mediation\n , the latest versions of your mediation adapters.\n\n- The inline adaptive banner sizes are designed to work best when using the\n full available width. In most cases, this will be the full width of the\n screen of the device in use. Be sure to take into account applicable safe areas.\n\n- The methods for getting the ad size are\n\n - `AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(int width)`\n - `AdSize.getLandscapeInlineAdaptiveBannerAdSize(int width)`\n - `AdSize.getPortraitInlineAdaptiveBannerAdSize(int width)`\n - `AdSize.getInlineAdaptiveBannerAdSize(int width, int maxHeight)`\n- When using the inline adaptive banner APIs, Google Mobile Ads SDK\n returns an `AdSize` with the given width and an\n inline flag. The height is either zero or `maxHeight`, depending on which\n API you're using. The actual height of the ad is made available when it's\n returned.\n\n- An inline adaptive banner is designed to be placed in scrollable content. The\n banner can be as tall as the device screen or limited by a maximum height,\n depending on the API.\n\nImplementation\n\nFollow the steps below to implement a simple inline adaptive banner.\n\n1. **Get an inline adaptive banner ad size.** The size you get will be used to request the adaptive banner. To get the adaptive ad size make sure that you:\n 1. Get the width of the device in use in density independent pixels, or set your own width if you don't want to use the full width of the screen. You can use `MediaQuery.of(context)` to get the screen width.\n 2. Use the appropriate static methods on the ad size class, such as `AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(int width)` to get an inline adaptive `AdSize` object for the current orientation.\n 3. If you wish to limit the height of the banner, you can use the static method `AdSize.getInlineAdaptiveBannerAdSize(int width, int maxHeight)`.\n2. Create a `BannerAd` object with your ad unit ID, the adaptive ad size, and an ad request object.\n3. Load the ad.\n4. In the `onAdLoaded` callback, use `BannerAd.getPlatformAdSize()` to get the updated platform ad size and update the `AdWidget` container height.\n\nCode example\n\nHere's an example widget that loads an inline adaptive banner to fit the\nwidth of the screen, accounting for insets: \n\n import 'package:flutter/material.dart';\n import 'package:google_mobile_ads/google_mobile_ads.dart';\n\n /// This example demonstrates inline adaptive banner ads.\n ///\n /// Loads and shows an inline adaptive banner ad in a scrolling view,\n /// and reloads the ad when the orientation changes.\n class InlineAdaptiveExample extends StatefulWidget {\n @override\n _InlineAdaptiveExampleState createState() =\u003e _InlineAdaptiveExampleState();\n }\n\n class _InlineAdaptiveExampleState extends State\u003cInlineAdaptiveExample\u003e {\n static const _insets = 16.0;\n BannerAd? _inlineAdaptiveAd;\n bool _isLoaded = false;\n AdSize? _adSize;\n late Orientation _currentOrientation;\n\n double get _adWidth =\u003e MediaQuery.of(context).size.width - (2 * _insets);\n\n @override\n void didChangeDependencies() {\n super.didChangeDependencies();\n _currentOrientation = MediaQuery.of(context).orientation;\n _loadAd();\n }\n\n void _loadAd() async {\n await _inlineAdaptiveAd?.dispose();\n setState(() {\n _inlineAdaptiveAd = null;\n _isLoaded = false;\n });\n\n // Get an inline adaptive size for the current orientation.\n AdSize size = AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(\n _adWidth.truncate());\n\n _inlineAdaptiveAd = BannerAd(\n // TODO: replace this test ad unit with your own ad unit.\n adUnitId: 'ca-app-pub-3940256099942544/9214589741',\n size: size,\n request: AdRequest(),\n listener: BannerAdListener(\n onAdLoaded: (Ad ad) async {\n print('Inline adaptive banner loaded: ${ad.responseInfo}');\n\n // After the ad is loaded, get the platform ad size and use it to\n // update the height of the container. This is necessary because the\n // height can change after the ad is loaded.\n BannerAd bannerAd = (ad as BannerAd);\n final AdSize? size = await bannerAd.getPlatformAdSize();\n if (size == null) {\n print('Error: getPlatformAdSize() returned null for $bannerAd');\n return;\n }\n\n setState(() {\n _inlineAdaptiveAd = bannerAd;\n _isLoaded = true;\n _adSize = size;\n });\n },\n onAdFailedToLoad: (Ad ad, LoadAdError error) {\n print('Inline adaptive banner failedToLoad: $error');\n ad.dispose();\n },\n ),\n );\n await _inlineAdaptiveAd!.load();\n }\n\n /// Gets a widget containing the ad, if one is loaded.\n ///\n /// Returns an empty container if no ad is loaded, or the orientation\n /// has changed. Also loads a new ad if the orientation changes.\n Widget _getAdWidget() {\n return OrientationBuilder(\n builder: (context, orientation) {\n if (_currentOrientation == orientation &&\n _inlineAdaptiveAd != null &&\n _isLoaded &&\n _adSize != null) {\n return Align(\n child: Container(\n width: _adWidth,\n height: _adSize!.height.toDouble(),\n child: AdWidget(\n ad: _inlineAdaptiveAd!,\n ),\n ));\n }\n // Reload the ad if the orientation changes.\n if (_currentOrientation != orientation) {\n _currentOrientation = orientation;\n _loadAd();\n }\n return Container();\n },\n );\n }\n\n @override\n Widget build(BuildContext context) =\u003e Scaffold(\n appBar: AppBar(\n title: Text('Inline adaptive banner example'),\n ),\n body: Center(\n child: Padding(\n padding: const EdgeInsets.symmetric(horizontal: _insets),\n child: ListView.separated(\n itemCount: 20,\n separatorBuilder: (BuildContext context, int index) {\n return Container(\n height: 40,\n );\n },\n itemBuilder: (BuildContext context, int index) {\n if (index == 10) {\n return _getAdWidget();\n }\n return Text(\n 'Placeholder text',\n style: TextStyle(fontSize: 24),\n );\n },\n ),\n ),\n ));\n\n @override\n void dispose() {\n super.dispose();\n _inlineAdaptiveAd?.dispose();\n }\n }"]]