跳转到内容

横幅和 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();

自适应横幅

Adaptive banners are responsive ads that dynamically adjust their dimensions based on device type and available width. Adaptive banners can either be anchored or inline, with each type serving specific integration needs.

Starting in MAX SDK version 13.2.0, you can integrate adaptive banners by initializing your MaxAdView with a MaxAdViewConfiguration object.

Anchored Adaptive Banners

Anchored adaptive banners are those you anchor at the top or bottom of the screen. They dynamically adjust their height based on the device type and the banner width.

You must set the height of the MaxAdView to the value returned by MaxAdFormat.BANNER.getAdaptiveSize( Context ).getHeight() instead of using a constant value like 50 or 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();
}

设置自定义宽度

For more specific integrations, you can configure a custom width in dp by calling a MaxAdViewConfiguration builder method (as of Google adapter version 21.5.0.3, Google Ad Manager adapter version 21.5.0.2, and Liftoff Monetize adapter version 7.4.3.2). To fetch the appropriate height for your custom anchored adaptive ad, call the adaptive size 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 );

内嵌自适应横幅

Adaptive banners are anchored by default. Alternatively, you can enable inline adaptive banners, which you can place in scrollable content. Inline adaptive banners are typically larger than anchored adaptive banners. They have variable heights that can extend to the full height of the device screen.

Inline adaptive banners are supported starting with Android Google adapter version 23.2.0.1 and Liftoff Monetize adapter version 7.4.3.2. To enable inline adaptive banners, set the MaxAdViewConfiguration adaptive type to MaxAdViewConfiguration.AdaptiveType.INLINE as shown in the code below:

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

The default maximum height for an inline adaptive ad is the entire height of the device screen. You may want to set a maximum height, in dp, for your inline adaptive ad to ensure that the ad is within the height of the MaxAdView. You can do this with code like the following, which uses a maximum height of 100 dp as an example:

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

Inline Adaptive MRECs

You can integrate adaptive MRECs, starting with Android Google adapters version 23.6.0.3 and Liftoff Monetize adapter version 7.4.3.2. You must set inline adaptive MRECs to the type MaxAdViewConfiguration.AdaptiveType.INLINE for them to function properly.

Inline adaptive MRECs span the full width of the application window by default, but you may optionally specify a custom width in dp. The height is variable and can extend beyond standard MREC dimensions up to the full height of the device screen if you do not specify a maximum height. You configure an inline adaptive MREC as shown in the following example:

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

Handling Adaptive Ad Sizes

The adaptive ad you load could be smaller than the dimensions you requested. You may wish to configure your UI in a way that can adapt based on the size of the adaptive ad served. If so, you can retrieve the width and height of the loaded ad, in dp, with code like the following:

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