AI Search

横幅广告

笔记

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

设置横幅广告

  1. 从左侧导航栏中选择“库存” >“库存设置”
  2. 搜索您想要为其创建广告位的应用或网站,然后点击+ 添加广告位
  3. 点击“选择广告单元”,然后选择“横幅广告”
  4. 点击“添加广告位”开始设置横幅广告。创建广告位后,即可查看广告位 ID。

将横幅广告添加到您的应用

创建横幅广告

方案一:在 XML 布局资源文件中添加横幅广告

您可以在 XML 布局资源文件中添加横幅广告,并像在常规视图中一样在代码中引用它。

以下代码片段展示了如何在 XML 中添加横幅广告。请注意,您需要将 InMobi 广告命名空间添加到顶级布局元素中才能添加横幅广告。

<androidx.constraintlayout.widget.constraintlayout
    xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:app="http://"schemas.android.com/apk/res-auto"
    xmlns:ads="http://"schemas.android.com/apk/lib/com.inmobi.ads"
    xmlns:tools="http://"schemas.android.com/tools"

对于横幅广告 XML 集成,使用 Android SDK 720 及更高版本的发布商需要将“plid-”附加到placementIdTAG,如下所示:

ads:placementId="plid-1431977751489005"

现在,您可以在代码中引用该横幅广告:

Java

InMobiBanner bannerAd = (InMobiBanner)findViewById(R.id.banner);

Kotlin

val bannerAd:InMobiBanner = findViewById(R.id.banner)

获得横幅实例后,您可以按照以下各节所示请求加载广告。

在此之前,请InMobiSdk.init(Context, String)在您的主活动或显示横幅广告的活动中调用该方法来初始化 InMobi SDK。

选项二:通过代码添加横幅广告

要在 Android 代码中添加横幅广告,请创建以下实例InMobiBanner

Java

InMobiBanner bannerAd = new InMobiBanner(BannerAdsActivity.this, 1468078426600L); 

Kotlin

val bannerAd = InMobiBanner(this@BannerAdsActivity, 1468078426600L)

笔记

  • 该类InMobiBanner 不是线程安全的。横幅实例必须在 UI 线程上创建。
  • 同样,此实例上的所有方法都必须在 UI 线程上调用。否则会导致不可预测的行为,并可能影响您使用 InMobi 进行盈利的能力。
  • 在 SDK 初始化之前创建 InMobiBanner 对象会引发异常。

创建横幅广告后,您可以将其添加到视图层级结构中:

Java

RelativeLayout adContainer = (RelativeLayout) findViewById(R.id.ad_container);
RelativeLayout.LayoutParams bannerLp = new RelativeLayout.LayoutParams( < WIDTH_IN_PIXEL > , < HEIGHT_IN_PIXEL > );
bannerLp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
bannerLp.addRule(RelativeLayout.CENTER_HORIZONTAL);
adContainer.addView(bannerAd, bannerLayoutParams);

Kotlin

val adContainer:RelativeLayout = findViewById(R.id.ad_container) 
val bannerLp:RelativeLayout.LayoutParams = RelativeLayout.LayoutParams( < WIDTH_IN_PIXEL > , < HEIGHT_IN_PIXEL > ) 
bannerLp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM) 
bannerLp.addRule(RelativeLayout.CENTER_HORIZONTAL) 
adContainer.addView(bannerAd, bannerLayoutParams)

笔记

  • 设置横幅广告布局参数时,应以像素为单位指定横幅广告的视图尺寸。
  • WRAP_CONTENT此外,将布局参数作为横幅广告的布局参数是编程错误。无论您是在 XML 布局资源文件中创建横幅广告,还是在代码中创建,此限制都适用。
  • 如果您要同时为手机和平板电脑开发应用,可以MATCH_PARENT将横幅广告的布局参数提供给 InMobi SDK。InMobi SDK 将正确计算横幅广告的视图尺寸,并使用这些信息从 InMobi 广告网络中获取广告。

正在加载横幅广告

加载横幅广告需要先将横幅广告视图添加到视图层级结构中,然后再向实例请求广告InMobiBanner。将视图添加到视图层级结构后,InMobi SDK 才能正确计算横幅广告视图的尺寸,并从网络中请求与这些尺寸最匹配的广告。

将视图添加InMobiBanner到视图层次结构后,就可以调用load()刚刚创建的实例上的方法来请求广告。

优化

设置横幅广告自动刷新

首次通过调用实例请求广告后,横幅广告会自动刷新load()InMobiBanner您可以在创建横幅广告时(在 XML 布局资源文件中)设置自动刷新,也可以稍后在代码中通过调用setRefreshInterval(int)横幅广告的相应方法来设置。

以下示例展示了如何在 XML 中实现此功能:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/lib/com.inmobi.ads" 
    xmlns:tools="http://schemas.android.com/tools"  
android:layout_width="match_parent" 
    android:layout_height="match_parent"  
   ...
   >  
     
    <com.inmobi.ads.InMobiBanner 
        android:layout_width="320dp" 
        android:layout_height="50dp" 
        android:id="@+id/banner" 
        ads:placementId="plid-1431977751489005" 
ads:refreshInterval="60" 
    /> 
</LinearLayout>

动画横幅广告刷新

您可以选择横幅刷新时生效的动画样式。您也可以选择关闭横幅刷新时的任何动画效果。

为此,您可以setAnimationType(InMobiBanner.AnimationType)在获取横幅实例后调用该方法。

笔记

默认动画已启用AnimationType.ROTATE_HORIZONTAL_AXIS。如果要关闭此功能,请调用setAnimationType带有AnimationType.ANIMATION_OFF参数的方法。

监听横幅广告生命周期事件

如果您想监听横幅生命周期中的关键事件,则需要实现该BannerAdListener接口。然后,您可以使用实例setListener(BannerAdListener)上的相应方法来设置此监听器InMobiBanner。每个事件的简要说明如下。

Java

/**
* A listener for receiving notifications during the lifecycle of a banner ad.
*/
public abstract class BannerAdEventListener {
    /**  
     * Called to notify that an ad was received successfully but is not ready to be displayed yet.  
     *  
     * @param ad   Represents the ad which was loaded or preloaded  
     * @param info Represents the ad meta information  
     */ 
    public void onAdFetchSuccessful(@NonNull InMobiBanner ad, @NonNull AdMetaInfo info) {} 
    /**  
     * Called to notify that an ad was successfully loaded.  
     * @param ad Represents the {@link InMobiBanner} ad which was loaded  
     * @param info Represents the ad meta information  
     */ 
    public void onAdLoadSucceeded(@NonNull InMobiBanner ad, @NonNull AdMetaInfo info) {} 
    /**  
     * Called to notify that a request to load an ad failed.  
     * @param ad Represents the {@link InMobiBanner} ad which failed to load  
     * @param status Represents the {@link InMobiAdRequestStatus} status containing error reason
     */ 
    public void onAdLoadFailed(@NonNull InMobiBanner ad, @NonNull InMobiAdRequestStatus status) {} 
    /**  
     * Called to notify that the user interacted with the ad.  
     * @param ad Represents the {@link InMobiBanner} ad on which user clicked  
     * @param params Represents the click parameters  
     */ 
    public void onAdClicked(@NonNull InMobiBanner ad, Map < Object, Object > params) {} 
    /**  
     * Called to notify that the banner ad was displayed  
     * @param ad Represents the {@link InMobiBanner} ad which was displayed  
     */ 
    public void onAdDisplayed(@NonNull InMobiBanner ad) {} 
    /**  
     * Called to notify that the User is about to return to the application after closing the ad.  
     * @param ad Represents the {@link InMobiBanner} ad which was closed  
     */ 
    public void onAdDismissed(@NonNull InMobiBanner ad) {} 
    /**  
     * Called to notify that the user is about to leave the application as a result of interacting with the ad.  
     * @param ad Represents the {@link InMobiBanner} ad  
     */ 
    public void onUserLeftApplication(@NonNull InMobiBanner ad) {} 
    /**  
     * Called to notify that a reward was unlocked.  
     * @param ad Represents the {@link InMobiBanner} ad for which rewards was unlocked  
     * @param rewards Represents the rewards unlocked  
     */ 
    public void onRewardsUnlocked(@NonNull InMobiBanner ad, Map < Object, Object > rewards) {} 
    /** 
     * Called to notify that inmobi has logged an impression for the ad 
     * @param ad     Represents the ad which was impressed 
     */ 
    public void onAdImpression(@NonNull InMobiBanner ad) { 
    } 
}

Kotlin

/**
* A listener for receiving notifications during the lifecycle of a banner ad.
*/
abstract class BannerAdEventListener : AdEventListener<inmobibanner>() {
/**
    * Called to notify that an ad preload has failed.  
    *  
    ***Note** This notification is given only when you use `preload()` in  
    * [InMobiBanner.preloadManager]  
    *  
    * @param ad     Represents the [InMobiBanner] ad which was preloaded  
    * @param status Represents the [InMobiAdRequestStatus] status containing error reason 
    */  
open fun onAdFetchFailed(ad: InMobiBanner, status: InMobiAdRequestStatus) {}
/**
    * Called to notify that the banner ad has expanded  
    *  
    * @param ad Represents the [InMobiBanner] ad which was expanded  
    */  
open fun onAdDisplayed(ad: InMobiBanner) {}
/**
    * Called to notify that the User is about to return to the application after closing the ad.  
    *  
    * @param ad Represents the [InMobiBanner] ad which was closed  
    */  
open fun onAdDismissed(ad: InMobiBanner) {}
/**
    * Called to notify that the user is about to leave the application as a result of interacting with the ad.  
    *  
    * @param ad Represents the [InMobiBanner] ad  
    */  
open fun onUserLeftApplication(ad: InMobiBanner) {}
/**
    * Called to notify that a reward was unlocked.  
    *  
    * @param ad      Represents the [InMobiBanner] ad for which rewards was unlocked  
    * @param rewards Represents the rewards unlocked  
    */  
open fun onRewardsUnlocked(ad: InMobiBanner, rewards: Map<any, any="">) {}
    /** 
     * Called to notify that inmobi has logged an impression for the ad 
     * @param ad     Represents the ad which was impressed 
     */ 
open fun onAdImpression(ad: InMobiBanner) {}
}

弃用横幅

如果您已不再使用 Banner 对象,只需对其调用 deprecate 方法即可释放资源。这将执行以下操作:

  1. 从视图层级结构中移除广告视图。
  2. 取消任何正在进行的刷新。
  3. 取消监听器引用。
  4. 其他清理工作。

Java

bannerAd.destroy(); // bannerAd is an example instance.

Kotlin

bannerAd.destroy() // bannerAd is an example instance.

本页内容

最后更新于 : 10 Sep, 2026