【发布时间】:2014-02-16 14:59:23
【问题描述】:
我有一个布局相对简单的 AdView 元素,它的显示/隐藏取决于用户是否启用了广告,以及是否有任何广告可用。
包含AdView的RelativeLayout默认是隐藏的,只有在调用onAdLoaded()时才会显示给用户。如果用户禁用了广告,或者如果调用了 onAdFailedToLoad(),则 RelativeLayout 保持隐藏状态(或者如果之前由于某种原因显示,则重新隐藏)。
这在大多数设备上都可以正常工作,但对于少数设备(包括三星 Galaxy Ace)来说就会出现问题。关闭广告一切都很好,但如果打开广告,则 AdView 会占用适当的空间,但完全是空白的(它不是黑色的,它只是完全不可见)。
这是布局文件的相关部分:
<RelativeLayout
android:id="@+id/adContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:visibility="gone"
style="@style/layout_row">
<!-- AdView is inserted here -->
</RelativeLayout>
@style/layout_row 只是在顶部添加一点边距以使其远离屏幕的最顶部,但仅此而已。
这是创建和插入广告的代码部分(在 onResume() 内部调用):
// hide advert by default
findViewById(R.id.adContainer).setVisibility(View.GONE);
// check if ads are turned on
if (prefs.getBoolean("show_ads", true)) {
// create a new ad view
AdView adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("___ MY ID ___");
// add it to the relative layout
((RelativeLayout) findViewById(R.id.adContainer)).addView(adView);
// deal with the arrival of ads
adView.setAdListener(new AdListener() {
// hide ad block if none could be found
@Override
public void onAdFailedToLoad(int errorCode) {
findViewById(R.id.adContainer).setVisibility(View.GONE);
super.onAdFailedToLoad(errorCode);
}
// show ad block if one was found
@Override
public void onAdLoaded() {
findViewById(R.id.adContainer).setVisibility(View.VISIBLE);
super.onAdLoaded();
}
});
// request ads
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR) // Emulators
.addTestDevice("8AFD3F7DD723DA9355EE3B35E8666475") // My Samsung S4 test device
.build();
adView.loadAd(adRequest);
} else {
// hide the ad block
findViewById(R.id.adContainer).setVisibility(View.GONE);
}
重复一遍,这在大多数设备上都可以正常工作,包括将广告设置为测试模式的设备和未设置广告的设备。到目前为止,问题只出现在 Galaxy Ace 上,但不幸的是我无法访问该设备来读取 logcat。
任何人都可以就可能导致此问题的原因以及可以采取的解决方法向我提供任何建议吗?
谢谢:)
更新:我在RelativeLayout 上设置了亮粉色背景以查看发生了什么,我发现在有问题的设备上,布局正在显示 - 广告的大小和形状中有一个粉红色的大块,就在广告应该在的位置。这只能表示正在调用onAdLoaded(),但仍然没有显示广告。有什么想法吗?
【问题讨论】:
-
谢谢@azhar - 我忘记了 android-layout 标签! :)
标签: android android-layout admob