【发布时间】:2014-06-17 12:49:16
【问题描述】:
我正在尝试使用新的 google play services sdk 将 admob 集成到我的 android 应用程序中。但是广告是不可见的。它在 log cat 中显示警告 > 没有足够的空间来展示广告。需要 480X75 像素,但只有 540X0。我使用了与 android 开发者页面上相同的代码。
code added in manifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|
orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
main.xml 中的代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:id="@+id/adViewl"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="match_parent"
ads:adSize="BANNER"
ads:adUnitId="AD_UNIT_ID" />
</LinearLayout>
和java代码
LinearLayout layout = (LinearLayout) findViewById(R.id.adViewl);
AdView adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("AD_UNIT_ID");
layout.addView(adView);
//AdRequest adRequest = new AdRequest.Builder().build(); // Start
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
请告诉我上面的代码哪里错了。
【问题讨论】:
-
我在 XML 中设置时出错,并且以编程方式同时吃掉了 AdSize.BANNER。 (错误如:AdUnitId 只能设置一次)。尝试删除
adView.setAdSize(AdSize.BANNER);和adView.setAdUnitId("AD_UNIT_ID");。还要将android:layout_height='wrap_content'放在adView 和lineareLayout 上。 -
显示整个 main.xml 文件。
-
@William main.xml 仅包含 AdView 和 LinearLayout 代码。
-
将 BANNER 更改为 adView.setAdSize(AdSize.SMART_BANNER);
-
顺便说一句,您是否为您的 adView 提供了单元 ID?还是它只是空的?
标签: android admob google-play-services