android - 两个 AdMob 添加 - 集成问题

标签 android admob

所以我只是将我的 AdMob .jar 文件集成到我的项目中。当我将 AdMob 文档中的一些代码写入我的项目时:

// Create the adView
        adView = new AdView(this, AdSize.BANNER,
                "MY-ID");

        // Lookup your LinearLayout assuming it's been given
        // the attribute android:id="@+id/mainLayout"
        LinearLayout layout = (LinearLayout) findViewById(R.id.mainLayout);

        // Add the adView to it
        layout.addView(adView);

        // Initiate a generic request to load it with an ad
        adView.loadAd(new AdRequest());

当我向我的 AndroidManifest.xml 添加所需的权限时:

<activity
            android:name="com.google.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />

最后,我将实际的广告横幅添加到我的 main_activity.xml 中:

<LinearLayout
        xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:id="@+id/mainLayout" >

        <com.google.ads.AdView
            android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ads:adSize="BANNER"
            ads:adUnitId="MY-ID"
            ads:loadAdOnCreate="true" >
        </com.google.ads.AdView>

    </LinearLayout>

但这有一个问题。在我的实际布局预览中,它只显示一个广告,但当我在我的设备上启动我的应用程序时,我得到了 两个 广告横幅。也许有人会知道这有什么问题?

最佳答案

这是因为 activity 布局预览 没有显示或处理 java 文件。就像如果您在 class 中更改背景,您将不会在 layout preview 中预览它。

// Create the adView
        adView = new AdView(this, AdSize.BANNER,
                "MY-ID");

        // Lookup your LinearLayout assuming it's been given
        // the attribute android:id="@+id/mainLayout"
        LinearLayout layout = (LinearLayout) findViewById(R.id.mainLayout);

        // Add the adView to it
        layout.addView(adView);

        // Initiate a generic request to load it with an ad
        adView.loadAd(new AdRequest());

这段代码是您在 class 中添加 adView 的代码,已完成,已添加。

<com.google.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ads:adSize="BANNER"
    ads:adUnitId="MY-ID"
    ads:loadAdOnCreate="true" >
</com.google.ads.AdView>

那么您将在 xml 中添加另一个 adView,因此 layout preview 将显示它。

现在您有两个 adViews。一个在您的 class 中设置,另一个在您的 xml 中设置。


如果你只想显示一个,你可以删除其中一个,如果你想同时显示和显示在你的布局预览中,删除你的java类,并添加另一个 xml

关于android - 两个 AdMob 添加 - 集成问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19819867/

相关文章:

android - 添加谷歌播放服务后,我的 APK 大小从 65 KB 增加到 8.8 MB(用于 admob)

ios - 如何在 ios 的计时器上放置 Admob 插页式广告?

java - 蓝牙通讯器

android - READ_EXTERNAL_STORAGE 不适用于 Android 10

android - 我可以在 google play 控制台中的应用程序中添加标签吗?

android - GCM 的 Unity Android 插件无法在接收时启动服务 Intent

android - setProgress 不适用于 android progressBar

android - 无法解析符号 'MobileAds'

android - Admob 停止虚假点击 : or don't show ads again if user click on ads

Android:使用 admob 在 WebView 中阻止 Adsense 广告