您可以在这里查看GitHub上的横幅广告集成代码示例。

您可以在 XML 布局资源文件中添加横幅广告,并像在常规视图中一样在代码中引用它。
以下代码片段展示了如何在 XML 中添加横幅广告。请注意,您需要将 InMobi 广告命名空间添加到顶级布局元素中才能添加横幅广告。
<span class="hljs-tag"><<span class="hljs-name">androidx.constraintlayout.widget.constraintlayout</span>
<span class="hljs-attr">xmlns:android</span>=<span class="hljs-string">"http://schemas.android.com/apk/res/android"</span>
<span class="hljs-attr">xmlns:app</span>=<span class="hljs-string">"http://"</span><span class="hljs-attr">schemas.android.com</span>/<span class="hljs-attr">apk</span>/<span class="hljs-attr">res-auto</span>"
<span class="hljs-attr">xmlns:ads</span>=<span class="hljs-string">"http://"</span><span class="hljs-attr">schemas.android.com</span>/<span class="hljs-attr">apk</span>/<span class="hljs-attr">lib</span>/<span class="hljs-attr">com.inmobi.ads</span>"
<span class="hljs-attr">xmlns:tools</span>=<span class="hljs-string">"http://"</span><span class="hljs-attr">schemas.android.com</span>/<span class="hljs-attr">tools</span>"</span>
对于横幅广告 XML 集成,使用 Android SDK 720 及更高版本的发布商需要将“plid-”附加到placementIdTAG,如下所示:
ads:placementId="plid-1431977751489005"
现在,您可以在代码中引用该横幅广告:
<span class="hljs-type">InMobiBanner</span> <span class="hljs-variable">bannerAd</span> <span class="hljs-operator">=</span> (InMobiBanner)findViewById(R.id.banner);
val bannerAd:InMobiBanner = findViewById(R.<span class="hljs-built_in">id</span>.banner)
获得横幅实例后,您可以按照以下各节所示请求加载广告。
在此之前,请InMobiSdk.init(Context, String)在您的主活动或显示横幅广告的活动中调用该方法来初始化 InMobi SDK。
要在 Android 代码中添加横幅广告,请创建以下实例InMobiBanner:
<span class="hljs-type">InMobiBanner</span> <span class="hljs-variable">bannerAd</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">InMobiBanner</span>(BannerAdsActivity.<span class="hljs-built_in">this</span>, <span class="hljs-number">1468078426600L</span>);
<span class="hljs-keyword">val</span> bannerAd = InMobiBanner(<span class="hljs-keyword">this</span><span class="hljs-symbol">@BannerAdsActivity</span>, <span class="hljs-number">1468078426600L</span>)
InMobiBanner 不是线程安全的。横幅实例必须在 UI 线程上创建。创建横幅广告后,您可以将其添加到视图层级结构中:
<span class="hljs-type">RelativeLayout</span> <span class="hljs-variable">adContainer</span> <span class="hljs-operator">=</span> (RelativeLayout) findViewById(R.id.ad_container);
RelativeLayout.<span class="hljs-type">LayoutParams</span> <span class="hljs-variable">bannerLp</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">RelativeLayout</span>.LayoutParams( < WIDTH_IN_PIXEL > , < HEIGHT_IN_PIXEL > );
bannerLp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
bannerLp.addRule(RelativeLayout.CENTER_HORIZONTAL);
adContainer.addView(bannerAd, bannerLayoutParams);
val adContainer:RelativeLayout = <span class="hljs-built_in">findViewById</span>(R.id.ad_container)
val bannerLp:RelativeLayout.LayoutParams = RelativeLayout.<span class="hljs-built_in">LayoutParams</span>( < WIDTH_IN_PIXEL > , < HEIGHT_IN_PIXEL > )
bannerLp.<span class="hljs-built_in">addRule</span>(RelativeLayout.ALIGN_PARENT_BOTTOM)
bannerLp.<span class="hljs-built_in">addRule</span>(RelativeLayout.CENTER_HORIZONTAL)
adContainer.<span class="hljs-built_in">addView</span>(bannerAd, bannerLayoutParams)
WRAP_CONTENT此外,将布局参数作为横幅广告的布局参数是编程错误。无论您是在 XML 布局资源文件中创建横幅广告,还是在代码中创建,此限制都适用。MATCH_PARENT将横幅广告的布局参数提供给 InMobi SDK。InMobi SDK 将正确计算横幅广告的视图尺寸,并使用这些信息从 InMobi 广告网络中获取广告。加载横幅广告需要先将横幅广告视图添加到视图层级结构中,然后再向实例请求广告InMobiBanner。将视图添加到视图层级结构后,InMobi SDK 才能正确计算横幅广告视图的尺寸,并从网络中请求与这些尺寸最匹配的广告。
将视图添加InMobiBanner到视图层次结构后,就可以调用load()刚刚创建的实例上的方法来请求广告。
首次通过调用实例请求广告后,横幅广告会自动刷新load()。InMobiBanner您可以在创建横幅广告时(在 XML 布局资源文件中)设置自动刷新,也可以稍后在代码中通过调用setRefreshInterval(int)横幅广告的相应方法来设置。
以下示例展示了如何在 XML 中实现此功能:
<span class="hljs-tag"><<span class="hljs-name">LinearLayout</span> <span class="hljs-attr">xmlns:android</span>=<span class="hljs-string">"http://schemas.android.com/apk/res/android"</span>
<span class="hljs-attr">xmlns:ads</span>=<span class="hljs-string">"http://schemas.android.com/apk/lib/com.inmobi.ads"</span>
<span class="hljs-attr">xmlns:tools</span>=<span class="hljs-string">"http://schemas.android.com/tools"</span>
<span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"match_parent"</span>
<span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"match_parent"</span>
<span class="hljs-attr">...</span>
></span>
<span class="hljs-tag"><<span class="hljs-name">com.inmobi.ads.InMobiBanner</span>
<span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"320dp"</span>
<span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"50dp"</span>
<span class="hljs-attr">android:id</span>=<span class="hljs-string">"@+id/banner"</span>
<span class="hljs-attr">ads:placementId</span>=<span class="hljs-string">"plid-1431977751489005"</span>
<span class="hljs-attr">ads:refreshInterval</span>=<span class="hljs-string">"60"</span>
/></span>
<span class="hljs-tag"></<span class="hljs-name">LinearLayout</span>></span>
您可以选择横幅刷新时生效的动画样式。您也可以选择关闭横幅刷新时的任何动画效果。
为此,您可以setAnimationType(InMobiBanner.AnimationType)在获取横幅实例后调用该方法。
默认动画已启用AnimationType.ROTATE_HORIZONTAL_AXIS。如果要关闭此功能,请调用setAnimationType带有AnimationType.ANIMATION_OFF参数的方法。
如果您想监听横幅生命周期中的关键事件,则需要实现该BannerAdListener接口。然后,您可以使用实例setListener(BannerAdListener)上的相应方法来设置此监听器InMobiBanner。每个事件的简要说明如下。
<span class="hljs-comment">/**
* A listener for receiving notifications during the lifecycle of a banner ad.
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">abstract</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">BannerAdEventListener</span> {
<span class="hljs-comment">/**
* Called to notify that an ad was received successfully but is not ready to be displayed yet.
*
* <span class="hljs-doctag">@param</span> ad Represents the ad which was loaded or preloaded
* <span class="hljs-doctag">@param</span> info Represents the ad meta information
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onAdFetchSuccessful</span><span class="hljs-params">(<span class="hljs-meta">@NonNull</span> InMobiBanner ad, <span class="hljs-meta">@NonNull</span> AdMetaInfo info)</span> {}
<span class="hljs-comment">/**
* Called to notify that an ad was successfully loaded.
* <span class="hljs-doctag">@param</span> ad Represents the {<span class="hljs-doctag">@link</span> InMobiBanner} ad which was loaded
* <span class="hljs-doctag">@param</span> info Represents the ad meta information
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onAdLoadSucceeded</span><span class="hljs-params">(<span class="hljs-meta">@NonNull</span> InMobiBanner ad, <span class="hljs-meta">@NonNull</span> AdMetaInfo info)</span> {}
<span class="hljs-comment">/**
* Called to notify that a request to load an ad failed.
* <span class="hljs-doctag">@param</span> ad Represents the {<span class="hljs-doctag">@link</span> InMobiBanner} ad which failed to load
* <span class="hljs-doctag">@param</span> status Represents the {<span class="hljs-doctag">@link</span> InMobiAdRequestStatus} status containing error reason
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onAdLoadFailed</span><span class="hljs-params">(<span class="hljs-meta">@NonNull</span> InMobiBanner ad, <span class="hljs-meta">@NonNull</span> InMobiAdRequestStatus status)</span> {}
<span class="hljs-comment">/**
* Called to notify that the user interacted with the ad.
* <span class="hljs-doctag">@param</span> ad Represents the {<span class="hljs-doctag">@link</span> InMobiBanner} ad on which user clicked
* <span class="hljs-doctag">@param</span> params Represents the click parameters
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onAdClicked</span><span class="hljs-params">(<span class="hljs-meta">@NonNull</span> InMobiBanner ad, Map < Object, Object > params)</span> {}
<span class="hljs-comment">/**
* Called to notify that the banner ad was displayed
* <span class="hljs-doctag">@param</span> ad Represents the {<span class="hljs-doctag">@link</span> InMobiBanner} ad which was displayed
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onAdDisplayed</span><span class="hljs-params">(<span class="hljs-meta">@NonNull</span> InMobiBanner ad)</span> {}
<span class="hljs-comment">/**
* Called to notify that the User is about to return to the application after closing the ad.
* <span class="hljs-doctag">@param</span> ad Represents the {<span class="hljs-doctag">@link</span> InMobiBanner} ad which was closed
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onAdDismissed</span><span class="hljs-params">(<span class="hljs-meta">@NonNull</span> InMobiBanner ad)</span> {}
<span class="hljs-comment">/**
* Called to notify that the user is about to leave the application as a result of interacting with the ad.
* <span class="hljs-doctag">@param</span> ad Represents the {<span class="hljs-doctag">@link</span> InMobiBanner} ad
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onUserLeftApplication</span><span class="hljs-params">(<span class="hljs-meta">@NonNull</span> InMobiBanner ad)</span> {}
<span class="hljs-comment">/**
* Called to notify that a reward was unlocked.
* <span class="hljs-doctag">@param</span> ad Represents the {<span class="hljs-doctag">@link</span> InMobiBanner} ad for which rewards was unlocked
* <span class="hljs-doctag">@param</span> rewards Represents the rewards unlocked
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onRewardsUnlocked</span><span class="hljs-params">(<span class="hljs-meta">@NonNull</span> InMobiBanner ad, Map < Object, Object > rewards)</span> {}
<span class="hljs-comment">/**
* Called to notify that inmobi has logged an impression for the ad
* <span class="hljs-doctag">@param</span> ad Represents the ad which was impressed
*/</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onAdImpression</span><span class="hljs-params">(<span class="hljs-meta">@NonNull</span> InMobiBanner ad)</span> {
}
}
<span class="hljs-comment">/**
* A listener for receiving notifications during the lifecycle of a banner ad.
*/</span>
<span class="hljs-keyword">abstract</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">BannerAdEventListener</span> : <span class="hljs-type">AdEventListener</span><<span class="hljs-type">inmobibanner</span>>() {
<span class="hljs-comment">/**
* Called to notify that an ad preload has failed.
*
***Note** This notification is given only when you use `preload()` in
* [InMobiBanner.preloadManager]
*
* <span class="hljs-doctag">@param</span> ad Represents the [InMobiBanner] ad which was preloaded
* <span class="hljs-doctag">@param</span> status Represents the [InMobiAdRequestStatus] status containing error reason
*/</span>
<span class="hljs-keyword">open</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">onAdFetchFailed</span><span class="hljs-params">(ad: <span class="hljs-type">InMobiBanner</span>, status: <span class="hljs-type">InMobiAdRequestStatus</span>)</span></span> {}
<span class="hljs-comment">/**
* Called to notify that the banner ad has expanded
*
* <span class="hljs-doctag">@param</span> ad Represents the [InMobiBanner] ad which was expanded
*/</span>
<span class="hljs-keyword">open</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">onAdDisplayed</span><span class="hljs-params">(ad: <span class="hljs-type">InMobiBanner</span>)</span></span> {}
<span class="hljs-comment">/**
* Called to notify that the User is about to return to the application after closing the ad.
*
* <span class="hljs-doctag">@param</span> ad Represents the [InMobiBanner] ad which was closed
*/</span>
<span class="hljs-keyword">open</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">onAdDismissed</span><span class="hljs-params">(ad: <span class="hljs-type">InMobiBanner</span>)</span></span> {}
<span class="hljs-comment">/**
* Called to notify that the user is about to leave the application as a result of interacting with the ad.
*
* <span class="hljs-doctag">@param</span> ad Represents the [InMobiBanner] ad
*/</span>
<span class="hljs-keyword">open</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">onUserLeftApplication</span><span class="hljs-params">(ad: <span class="hljs-type">InMobiBanner</span>)</span></span> {}
<span class="hljs-comment">/**
* Called to notify that a reward was unlocked.
*
* <span class="hljs-doctag">@param</span> ad Represents the [InMobiBanner] ad for which rewards was unlocked
* <span class="hljs-doctag">@param</span> rewards Represents the rewards unlocked
*/</span>
<span class="hljs-keyword">open</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">onRewardsUnlocked</span><span class="hljs-params">(ad: <span class="hljs-type">InMobiBanner</span>, rewards: <span class="hljs-type">Map</span><<span class="hljs-type">any</span>, any=<span class="hljs-string">""</span>>)</span></span> {}
<span class="hljs-comment">/**
* Called to notify that inmobi has logged an impression for the ad
* <span class="hljs-doctag">@param</span> ad Represents the ad which was impressed
*/</span>
<span class="hljs-keyword">open</span> <span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">onAdImpression</span><span class="hljs-params">(ad: <span class="hljs-type">InMobiBanner</span>)</span></span> {}
}
如果您已不再使用 Banner 对象,只需对其调用 deprecate 方法即可释放资源。这将执行以下操作:
bannerAd.destroy(); <span class="hljs-comment">// bannerAd is an example instance.</span>
bannerAd<span class="hljs-selector-class">.destroy</span>() <span class="hljs-comment">// bannerAd is an example instance.</span>
By installing this SDK update, you agree that your Children Privacy Compliance setting remains accurate or that you will update that setting, whenever there is a change in your app's audience. You may update the app's Children Privacy Compliance settings at https://publisher.inmobi.com/my-inventory/app-and-placements.
Support Center