banner 和 MREC 广告均为矩形的广告格式,会占据应用布局中的一部分,通常位于屏幕顶部或底部,有时也内嵌在可滚动的内容中。 用户与应用互动时,它们始终可见,既不会干扰或打断游戏和使用体验,也可以在指定时间周期后自动刷新。
以下各节将向您介绍如何加载、显示和隐藏 banner 或 MREC 广告。
要加载 banner 或 MREC,请创建一个与您的广告单元相对应的 MaxAdView 对象。
然后调用该对象的 loadAd() 方法。
要展示该广告,请将 MaxAdView 对象添加为视图层级结构的子视图。
实现 MaxAdViewAdListener,以便在广告准备就绪时收到通知 (您也会收到其他广告相关事件的通知)。
如果您的集成需要在内容流中展示 MREC 广告,AppLovin 推荐以下做法:
loadAd() 手动刷新内容 (重复使用 MaxAdView 实例)。您可以在 AppLovin 演示应用 (Java, Kotlin) 中找到示例实现。
public class ExampleActivity extends Activity
implements MaxAdViewAdListener
{
private MaxAdView adView;
void createBannerAd()
{
adView = new MaxAdView( "«ad-unit-ID»", this );
adView.setListener( this );
// Stretch to the width of the screen for banners to be fully functional
int width = ViewGroup.LayoutParams.MATCH_PARENT;
// Banner height on phones and tablets is 50 and 90, respectively
int heightPx = getResources().getDimensionPixelSize( R.dimen.banner_height );
adView.setLayoutParams( new FrameLayout.LayoutParams( width, heightPx ) );
// Set background color for banners to be fully functional
adView.setBackgroundColor( «background-color» );
ViewGroup rootView = findViewById( android.R.id.content );
rootView.addView( adView );
// Load the ad
adView.loadAd();
}
// MAX Ad Listener
@Override
public void onAdLoaded(final MaxAd maxAd) {}
@Override
public void onAdLoadFailed(final String adUnitId, final MaxError error) {}
@Override
public void onAdDisplayFailed(final MaxAd maxAd, final MaxError error) {}
@Override
public void onAdClicked(final MaxAd maxAd) {}
@Override
public void onAdExpanded(final MaxAd maxAd) {}
@Override
public void onAdCollapsed(final MaxAd maxAd) {}
@Override
public void onAdDisplayed(final MaxAd maxAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
@Override
public void onAdHidden(final MaxAd maxAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
}
class ExampleActivity : Activity(), MaxAdViewAdListener
{
private var adView: MaxAdView? = null
fun createBannerAd()
{
adView = MaxAdView("«ad-unit-ID»", this)
adView?.setListener(this)
// Stretch to the width of the screen for banners to be fully functional
val widthPx = ViewGroup.LayoutParams.MATCH_PARENT
// Banner height on phones and tablets is 50 and 90, respectively
val heightPx = resources.getDimensionPixelSize(R.dimen.banner_height)
adView?.layoutParams = FrameLayout.LayoutParams(widthPx, heightPx)
// Set background color for banners to be fully functional
adView?.setBackgroundColor(«background-color»)
val rootView = findViewById(android.R.id.content)
rootView.addView(adView)
// Load the ad
adView?.loadAd()
}
// MAX Ad Listener
override fun onAdLoaded(maxAd: MaxAd) {}
override fun onAdLoadFailed(adUnitId: String?, error: MaxError?) {}
override fun onAdDisplayFailed(ad: MaxAd?, error: MaxError?) {}
override fun onAdClicked(maxAd: MaxAd) {}
override fun onAdExpanded(maxAd: MaxAd) {}
override fun onAdCollapsed(maxAd: MaxAd) {}
override fun onAdDisplayed(maxAd: MaxAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
override fun onAdHidden(maxAd: MaxAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
}
public class ExampleActivity extends Activity
implements MaxAdViewAdListener
{
private MaxAdView adView;
void createMRecAd()
{
adView = new MaxAdView( "«ad-unit-ID»", MaxAdFormat.MREC, this );
adView.setListener( this );
// MREC width and height are 300 and 250 respectively, on phones and tablets
int widthPx = AppLovinSdkUtils.dpToPx( this, 300 );
int heightPx = AppLovinSdkUtils.dpToPx( this, 250 );
adView.setLayoutParams( new FrameLayout.LayoutParams( widthPx, heightPx ) );
// Set background color for MRECs to be fully functional
adView.setBackgroundColor( «background-color» );
ViewGroup rootView = findViewById( android.R.id.content );
rootView.addView( adView );
// Load the ad
adView.loadAd();
}
// MAX Ad Listener
@Override
public void onAdLoaded(final MaxAd maxAd) {}
@Override
public void onAdLoadFailed(final String adUnitId, final MaxError error) {}
@Override
public void onAdDisplayFailed(final MaxAd maxAd, final MaxError error) {}
@Override
public void onAdClicked(final MaxAd maxAd) {}
@Override
public void onAdExpanded(final MaxAd maxAd) {}
@Override
public void onAdCollapsed(final MaxAd maxAd) {}
@Override
public void onAdDisplayed(final MaxAd maxAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
@Override
public void onAdHidden(final MaxAd maxAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
}
class ExampleActivity : Activity(), MaxAdViewAdListener
{
private var adView: MaxAdView? = null
fun createMRecAd()
{
adView = MaxAdView("«ad-unit-ID»", MaxAdFormat.MREC, this)
adView?.setListener(this)
// MREC width and height are 300 and 250 respectively, on phones and tablets
val widthPx = AppLovinSdkUtils.dpToPx(this, 300)
val heightPx = AppLovinSdkUtils.dpToPx(this, 250)
adView?.layoutParams = FrameLayout.LayoutParams(widthPx, heightPx)
// Set background color for MRECs to be fully functional
adView?.setBackgroundColor(«background-color»)
val rootView = findViewById(android.R.id.content)
rootView.addView(adView)
// Load the ad
adView?.loadAd()
}
// MAX Ad Listener
override fun onAdLoaded(maxAd: MaxAd) {}
override fun onAdLoadFailed(adUnitId: String?, error: MaxError?) {}
override fun onAdDisplayFailed(ad: MaxAd?, error: MaxError?) {}
override fun onAdClicked(maxAd: MaxAd) {}
override fun onAdExpanded(maxAd: MaxAd) {}
override fun onAdCollapsed(maxAd: MaxAd) {}
override fun onAdDisplayed(maxAd: MaxAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
override fun onAdHidden(maxAd: MaxAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
}
您还可以将 MAX banner 或 MREC 广告添加到您的视图布局 XML 中。
请通过设置背景或背景颜色 (android:background) 来确保您的广告功能完整。
对于 banner 广告,请将宽度 (android:layout_width) 拉伸至屏幕宽度。
对于 MREC 广告,请相应地设置 android:adFormat:
<com.applovin.mediation.ads.MaxAdView
xmlns:maxads="http://schemas.applovin.com/android/1.0"
maxads:adUnitId="«ad-unit-ID»"
android:background="@color/banner_background_color"
android:layout_width="match_parent"
android:layout_height="@dimen/banner_height" />
在 res/values/attrs.xml 中声明 50 dp 的基础 banner 高度:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="banner_height">50dp</dimen>
</resources>
在 res/values-sw600dp/attrs.xml 中声明 90 dp 的平板电脑 banner 高度:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="banner_height">90dp</dimen>
</resources>
<com.applovin.mediation.ads.MaxAdView
xmlns:maxads="http://schemas.applovin.com/android/1.0"
maxads:adUnitId="«ad-unit-ID»"
maxads:adFormat="MREC"
android:background="@color/mrec_background_color"
android:layout_width="300dp"
android:layout_height="250dp" />
您仍必须在以此方式创建的 MaxAdView 上调用 loadAd():
MaxAdView adView = findViewById( R.id.ad_view );
adView.loadAd();
var adView: MaxAdView = findViewById(R.id.ad_view)
adView.loadAd()
如果您不再需要 MaxAdView 实例,请调用其 destroy() 方法以释放资源。
例如,如果用户购买了去广告服务,就可能需要这样做。
如果您使用多个具有相同广告单元 ID 的实例,请勿调用 destroy() 方法。
adView.destroy();
adView.destroy()
Google bidding 和 Google AdMob、Google Ad Manager、Liftoff Monetize、Pangle 以及 Yandex 支持自适应 banner。 MAX 会以非自适应方式处理来自其他 network 的 banner 尺寸。
Google 建议开发者在 banner 位置 and 应用内容之间留出 50 px 的内边距。 这可以降低用户意外点击 banner 的可能性。 请参阅 Google “关于确认点击” 的政策了解更多信息和最佳实践。
自适应 banner 是响应式广告,可根据设备类型和可用宽度动态调整其尺寸。 自适应 banner 可以是锚定式或内嵌式,每种类型都适用于特定的集成需求。
从 MAX SDK 13.2.0 版本开始,您可以通过在构建时设置了自适应类型的 MaxAdViewConfiguration 对象来初始化 MaxAdView,从而集成自适应 banner。
以下 adapter 支持特殊的自适应 banner 功能:
| network | adapter 版本 | 特殊功能 |
|---|---|---|
| Google Ad Manager | 23.2.0.1+ | 内嵌自适应 banner 广告 |
| 23.6.0.3+ | 内嵌自适应 MREC 广告 | |
| 21.5.0.2+ | 设置自定义宽度 | |
| Google bidding 和 Google AdMob | 23.2.0.1+ | 内嵌自适应 banner 广告 |
| 23.6.0.3+ | 内嵌自适应 MREC 广告 | |
| 21.5.0.3+ | 设置自定义宽度 | |
| InMobi | 11.3.0.1+ | |
| Liftoff Monetize | 7.4.3.2+ | |
| Pangle | 7.1.0.4.0+ | |
| Yandex | 7.12.2.1+ |
锚定式自适应 banner 是指固定在屏幕顶部或底部的广告。 它们会根据设备类型和 banner 宽度动态调整高度。
您必须将 MaxAdView 的高度设置为 MaxAdFormat.BANNER.getAdaptiveSize( Context ).getHeight() 返回的值,而不是使用 50 或 90 等常量值。
void createAnchoredAdaptiveBannerAd()
{
// Stretch to the width of the screen for banners to be fully functional
int widthPx = ViewGroup.LayoutParams.MATCH_PARENT;
// Get the anchored adaptive banner height
int heightDp = MaxAdFormat.BANNER.getAdaptiveSize( this ).getHeight();
int heightPx = AppLovinSdkUtils.dpToPx( this, heightDp );
MaxAdViewConfiguration config = MaxAdViewConfiguration.builder()
.setAdaptiveType( MaxAdViewConfiguration.AdaptiveType.ANCHORED )
.build();
adView = new MaxAdView( "«ad-unit-ID»", config );
adView.setListener( this );
adView.setLayoutParams( new FrameLayout.LayoutParams( widthPx, heightPx ) );
// Set background color for banners to be fully functional
adView.setBackgroundColor( «background-color» );
ViewGroup rootView = findViewById( android.R.id.content );
rootView.addView( adView );
// Load the ad
adView.loadAd();
}
fun createAnchoredAdaptiveBannerAd()
{
// Stretch to the width of the screen for banners to be fully functional
val widthPx = ViewGroup.LayoutParams.MATCH_PARENT
// Get the anchored adaptive banner height
val heightDp = MaxAdFormat.BANNER.getAdaptiveSize(this).height
val heightPx = AppLovinSdkUtils.dpToPx(this, heightDp)
val config = MaxAdViewConfiguration.builder()
.setAdaptiveType( MaxAdViewConfiguration.AdaptiveType.ANCHORED )
.build();
adView = MaxAdView("«ad-unit-ID»", config)
adView?.setListener(this)
adView?.layoutParams = FrameLayout.LayoutParams(widthPx, heightPx)
// Set background color for banners to be fully functional
adView?.setBackgroundColor(«background-color»)
val rootView = findViewById<ViewGroup>(android.R.id.content)
rootView.addView(adView)
// Load the ad
adView?.loadAd()
}
对于更具体的集成,您可以通过调用 MaxAdViewConfiguration 构建器方法来配置以 dp 为单位的自定义宽度。
要为自定义的锚定式自适应广告获取合适的高度,请调用自适应尺寸 API。
int widthDp = 400;
// Get the anchored adaptive banner height
int heightDp = MaxAdFormat.BANNER.getAdaptiveSize( widthDp, context ).getHeight();
MaxAdViewConfiguration config = MaxAdViewConfiguration.builder()
.setAdaptiveType( MaxAdViewConfiguration.AdaptiveType.ANCHORED )
.setAdaptiveWidth( widthDp )
.build();
adView = new MaxAdView( "«ad-unit-ID»", config );
val widthDp = 400
// Get the anchored adaptive banner height
val heightDp = adView.adFormat.getAdaptiveSize(widthDp, context).height
val config = MaxAdViewConfiguration.builder()
.setAdaptiveType(MaxAdViewConfiguration.AdaptiveType.ANCHORED)
.setAdaptiveWidth(widthDp)
.build()
adView = MaxAdView("«ad-unit-ID»", config)
自适应 banner 默认是锚定式的。 您也可以启用内嵌自适应 banner,并将其放置在可滚动的内容中。 内嵌自适应 banner 通常比锚定式自适应 banner 更大。 它们的高度是可变的,最高可延伸至设备屏幕的完整高度。
要启用内嵌自适应 banner,请将 MaxAdViewConfiguration 自适应类型设置为 MaxAdViewConfiguration.AdaptiveType.INLINE,如下面的代码所示:
MaxAdViewConfiguration config = MaxAdViewConfiguration.builder()
.setAdaptiveType( MaxAdViewConfiguration.AdaptiveType.INLINE )
⋮
.build();
val config = MaxAdViewConfiguration.builder()
.setAdaptiveType(MaxAdViewConfiguration.AdaptiveType.INLINE)
⋮
.build()
内嵌自适应广告的默认最大高度是设备屏幕的完整高度。
您可能希望为内嵌自适应广告设置一个以 dp 为单位的最大高度,以确保广告适合 MaxAdView 的高度。
您可以使用如下代码来实现,以下以 100 dp 的最大高度为例:
MaxAdViewConfiguration config = MaxAdViewConfiguration.builder()
.setAdaptiveType( MaxAdViewConfiguration.AdaptiveType.INLINE )
.setInlineMaximumHeight( 100 )
⋮
.build();
val config = MaxAdViewConfiguration.builder()
.setAdaptiveType(MaxAdViewConfiguration.AdaptiveType.INLINE)
.setInlineMaximumHeight( 100 )
⋮
.build()
内嵌自适应 MREC 默认会覆盖整个应用窗口的宽度,但您也可以选择指定以 dp 为单位的自定义宽度。 其高度是可变的,如果您未指定最大高度,则该高度可以超出标准的 MREC 尺寸,最高可达设备屏幕的完整高度。
配置内嵌自适应 MREC,并将自适应类型设置为 MaxAdViewConfiguration.AdaptiveType.INLINE,如以下示例所示:
void createInlineAdaptiveMRecAd()
{
// Set a custom width, in dp, for the inline adaptive MREC. Otherwise stretch to screen width by using ViewGroup.LayoutParams.MATCH_PARENT
int widthDp = 400;
int widthPx = AppLovinSdkUtils.dpToPx( this, widthDp );
// Set a maximum height, in dp, for the inline adaptive MREC. Otherwise use standard MREC height of 250 dp
// Google recommends a height greater than 50 dp, with a minimum of 32 dp and a maximum equal to the screen height
// The value must also not exceed the height of the MaxAdView
int heightDp = 300;
int heightPx = AppLovinSdkUtils.dpToPx( this, heightDp );
MaxAdViewConfiguration config = MaxAdViewConfiguration.builder()
.setAdaptiveType( MaxAdViewConfiguration.AdaptiveType.INLINE )
.setAdaptiveWidth( widthDp ) // Optional: The adaptive ad spans the width of the application window if you do not set a value
.setInlineMaximumHeight( heightDp ) // Optional: The maximum height is the screen height if you do not set a value
.build();
adView = new MaxAdView( "«ad-unit-ID»", MaxAdFormat.MREC, config );
adView.setListener( this );
adView.setLayoutParams( new FrameLayout.LayoutParams( widthPx, heightPx ) );
// Set background color for adaptive MRECs to be fully functional
adView.setBackgroundColor( «background-color» );
ViewGroup rootView = findViewById( android.R.id.content );
rootView.addView( adView );
// Load the ad
adView.loadAd();
}
fun createInlineAdaptiveMRecAd()
{
// Set a custom width, in dp, for the inline adaptive MREC. Otherwise stretch to screen width by using ViewGroup.LayoutParams.MATCH_PARENT
val widthDp = 400
val widthPx = AppLovinSdkUtils.dpToPx(this, widthDp)
// Set a maximum height, in dp, for the inline adaptive MREC. Otherwise use standard MREC height of 250 dp
// Google recommends a height greater than 50 dp, with a minimum of 32 dp and a maximum equal to the screen height
// The value must also not exceed the height of the MaxAdView
val heightDp = 300
val heightPx = AppLovinSdkUtils.dpToPx( this, heightDp )
val config = MaxAdViewConfiguration.builder()
.setAdaptiveType(MaxAdViewConfiguration.AdaptiveType.INLINE)
.setAdaptiveWidth(widthDp) // Optional: The adaptive ad spans the width of the application window if a value is not specified
.setInlineMaximumHeight(heightDp) // Optional: The maximum height is the screen height if a value is not specified
.build()
adView = MaxAdView("«ad-unit-ID»", MaxAdFormat.MREC, config)
adView?.setListener(this)
adView?.layoutParams = FrameLayout.LayoutParams(widthPx, heightPx)
// Set background color for adaptive MRECs to be fully functional
adView?.setBackgroundColor(«background-color»)
val rootView = findViewById(android.R.id.content)
rootView.addView(adView)
// Load the ad
adView?.loadAd()
}
您加载的自适应广告可能小于您请求的尺寸。 您可能希望配置您的 UI,使其能够根据所展示的自适应广告的尺寸进行自适应。 如果是这样,您可以使用如下代码获取已加载广告的宽度和高度 (以 dp 为单位):
@Override
public void onAdLoaded(final MaxAd maxAd)
{
AppLovinSdkUtils.Size adViewSize = maxAd.getSize();
int widthDp = adViewSize.getWidth();
int heightDp = adViewSize.getHeight();
⋮
}
override fun onAdLoaded(ad: MaxAd?)
{
val adViewSize = ad?.size!!
val widthDp = adViewSize.width
val heightDp = adViewSize.height
⋮
}
您可能希望停止广告的自动刷新。 当您隐藏广告或想要手动刷新时,可能会需要这个操作。 使用以下代码停止自动刷新:
// Set this extra parameter to work around SDK bug that ignores calls to stopAutoRefresh()
adView.setExtraParameter( "allow_pause_auto_refresh_immediately", "true" );
adView.stopAutoRefresh();
// Set this extra parameter to work around SDK bug that ignores calls to stopAutoRefresh()
adView.setExtraParameter( "allow_pause_auto_refresh_immediately", "true" )
adView.stopAutoRefresh()
使用以下代码启动广告的自动刷新:
adView.startAutoRefresh();
adView.startAutoRefresh()
通过调用 loadAd() 手动刷新广告内容。
只有在先停止自动刷新的情况下,您才能执行此操作。
adView.loadAd();
adView.loadAd()