跳转到内容

横幅和 MREC

横幅和 MREC 广告均为矩形的广告格式,会占据应用版面中的一部分,通常位于屏幕顶部或底部,有时也内嵌在可滚动的内容中。用户与应用互动时,横幅和 MREC 广告始终可见,既不会干扰或打断游戏和使用体验,也可以在指定时间周期后自动刷新。

以下各节将向您介绍如何加载、显示和隐藏横幅或 MREC 广告。

加载横幅或 MREC

要加载横幅或 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»" );
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 */ }
}

MREC

public class ExampleActivity extends Activity
implements MaxAdViewAdListener
{
private MaxAdView adView;
void createMRecAd
{
adView = new MaxAdView( "«ad-unit-ID»", MaxAdFormat.MREC );
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 */ }
}

在布局编辑器中加载并展示横幅或 MREC 广告

您也可以将 MAX 横幅或 MREC 广告添加到视图布局 XML 中。 设置背景或背景色 (android:background),确保广告功能齐全。 如果是横幅,请将宽度 (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 中声明基础横幅高度为 50dp:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="banner_height">50dp</dimen>
</resources>

res/values-sw600dp/attrs.xml 中声明平板电脑横幅高度为 90dp:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="banner_height">90dp</dimen>
</resources>

您还必须通过下列方式,在创建的 MaxAdView 上调用 loadAd():

MaxAdView adView = findViewById( R.id.ad_view );
adView.loadAd();

销毁横幅或 MREC 广告

如果您不再需要 MaxAdView 实例对象,请调用其 destroy() 方法释放资源。 例如,如果用户购买了广告移除服务,就可能发生这种情况。 如果您使用多个具有相同广告单元 ID 的实例,请不要调用 destroy() 方法。

adView.destroy();

自适应横幅

自适应横幅广告是响应式广告形式,会根据设备类型和可用宽度动态调整其尺寸。 自适应横幅可设置为锚定式 (anchored) 或内嵌式 (inline),每种类型适用于不同的集成需求。

从 MAX SDK 13.2.0 版本开始,您可以通过使用 MaxAdViewConfiguration 对象初始化 MaxAdView,来集成自适应横幅。

锚定式自适应横幅

锚定式自适应横幅是指固定在屏幕顶部或底部的广告形式,根据设备类型和横幅宽度动态调整高度。

您必须将 MaxAdView 的高度设置为 MaxAdFormat.BANNER.getAdaptiveSize( Context ).getHeight() 返回的值,而不是使用 50 或 90 等常量值。

void createAnchoredAdaptiveBannerAd()
{
// Stretch to the width of the screen for banners to be fully functional
int width = 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( 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();
}

设置自定义宽度

对于更具体的集成,您可以通过调用 MaxAdViewConfiguration 构建方法来配置以 dp 为单位的自定义宽度。该功能适用于 Google 适配器 21.5.0.3 版本、Google Ad Manager 适配器 21.5.0.2 版本和 Liftoff Monetize 适配器7.4.3.2 版本。要为自定义的锚定式自适应广告调取合适的高度,请调用自适应尺寸 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 = MaxAdView( "«ad-unit-ID»", config );

内嵌自适应横幅

自适应横幅默认为锚定式横幅。 您也可以启用内嵌自适应横幅,此类横幅用于滚动内容中。 内嵌自适应横幅通常比锚定自适应横幅尺寸更大。 其高度是可变的,可以扩展到设备屏幕的整个高度。

Android Google 适配器 23.2.0.1 版本和 Liftoff Monetize 适配器 7.4.3.2 版本及更新版本均支持内嵌自适应横幅。要启用内嵌自适应横幅,请将 MaxAdViewConfiguration 自适应类型设置为 MaxAdViewConfiguration.AdaptiveType.INLINE,如以下代码所示:

MaxAdViewConfiguration config = MaxAdViewConfiguration.builder()
.setAdaptiveType( MaxAdViewConfiguration.AdaptiveType.INLINE )
.build();

内嵌自适应广告的默认最大高度为设备屏幕的整个高度。您可以设置内嵌自适应广告的最大高度 (以 dp 为单位),确保广告位于 MaxAdView 的高度范围内。您可以使用如下代码来完成此操作,以 100 dp 的最大高度为例:

MaxAdViewConfiguration config = MaxAdViewConfiguration.builder()
.setAdaptiveType( MaxAdViewConfiguration.AdaptiveType.INLINE )
.setInlineMaximumHeight( 100 )
.build();

内嵌自适应 MREC

您可以集成自适应 MREC,该功能支持 Android Google 适配器 23.6.0.3 版本和 Liftoff Monetize 适配器 7.4.3.2 版本及更新版本。 您必须将内嵌自适应 MREC 设置为 MaxAdViewConfiguration.AdaptiveType.INLINE 类型,它们才能正常运行。

内嵌自适应 MREC 默认会覆盖整个应用程序窗口宽度,但您也可以指定自定义宽度 (以 dp 为单位)。该高度可变,如果您未指定最大高度,则该变量会超出标准的 MREC 广告尺寸,最高可达设备屏幕的完整高度。 按照以下示例,配置内嵌自适应 MREC:

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 will span the width of the application window if you do not set a value
.setInlineMaximumHeight( heightDp ) // Optional: The maximum height will be 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();
}

处理自适应广告尺寸

您加载的自适应广告可能小于您请求的尺寸。您可能希望根据所展示的自适应广告的尺寸,配置 UI 以实现相应的适配。您可以使用如下代码,获取已加载广告的宽度和高度 (以 dp 为单位):

@Override
public void onAdLoaded(final MaxAd maxAd)
{
AppLovinSdkUtils.Size adViewSize = maxAd.getSize();
int widthDp = adViewSize.getWidth();
int heightDp = adViewSize.getHeight();
}

停止和启动自动刷新

您可以停止广告的自动刷新。 当您隐藏广告或想要手动刷新时,可能会需要这个操作。 使用以下代码停止自动刷新:

// 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();

通过调用 loadAd() 手动刷新广告内容。 只有在之前已停止自动刷新的情况下,才能进行此操作。

adView.loadAd();