跳转到内容

横幅和 MREC

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

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

加载横幅或 MREC

To load a banner or MREC, create a MaxAdView object that corresponds to your ad unit. Then call that object’s loadAd() method. To show that ad, add the MaxAdView object as a subview of your view hierarchy. Implement MaxAdViewAdListener so that you are notified when your ad is ready (you will also be notified of other ad-related events).

如果您的集成需要在内容流中展示 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 广告

You can also add MAX banners or MRECs to your view layout XML. Ensure that your ads are fully functional by setting a background or background color (android:background). For banners, stretch the width (android:layout_width) to the width of the screen. For MRECs, set android:adFormat accordingly:

<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" />

Declare the base banner height of 50 dp in res/values/attrs.xml:

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

Declare the tablet banner height of 90 dp in res/values-sw600dp/attrs.xml:

<?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 be either anchored or inline, with each type serving specific integration needs.

从 MAX SDK 13.2.0 版本开始,您可以通过在构建时设置了自适应类型的 MaxAdViewConfiguration 对象,初始化 MaxAdView,从而集成自适应横幅。

广告平台适配器对自适应横幅功能的支持

以下适配器对特殊自适应横幅功能的支持:

平台特殊功能适配器版本
Google Ad Manager内嵌自适应横幅广告23.2.0.1+
内嵌自适应 MREC 广告23.6.0.3+
设置自定义宽度21.5.0.2+
Google 竞价和 Google AdMob内嵌自适应横幅广告23.2.0.1+
内嵌自适应 MREC 广告23.6.0.3+
设置自定义宽度21.5.0.3+
Liftoff Monetize内嵌自适应横幅广告7.4.3.2+
内嵌自适应 MREC 广告7.4.3.2+
设置自定义宽度7.4.3.2+

锚定式自适应横幅

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

您必须将 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 为单位的自定义宽度。要为自定义的锚定式自适应广告调取合适的高度,请调用自适应尺寸 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. You can also 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.

要启用内嵌自适应横幅,请将 MaxAdViewConfiguration 自适应类型设置为 MaxAdViewConfiguration.AdaptiveType.INLINE,如以下代码所示:

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 fits in 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();

内嵌自适应 MREC

内嵌自适应 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();
}

处理自适应广告尺寸

您加载的自适应广告可能小于您请求的尺寸。您可能希望根据所展示的自适应广告的尺寸,配置 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();

Manually refresh the ad contents by calling loadAd(). You can do this only if you first stop auto-refresh.

adView.loadAd();