Android AdMob:addView 在返回活动之前不会显示广告 [英] Android AdMob: addView doesn't show ad until return to activity

查看:25
本文介绍了Android AdMob:addView 在返回活动之前不会显示广告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在游戏顶部添加横幅广告.我的活动使用带有自定义 SurfaceView 的 RelativeLayout.我希望广告与 SurfaceView 重叠.

I'm trying to add a banner ad to the top of my game. My activity uses a RelativeLayout with a custom SurfaceView. I want the ad to overlap the SurfaceView.

广告加载并可以点击,但不会被绘制到屏幕上.当我离开活动并返回时,广告被绘制(例如,通过按下主页按钮,然后通过最近的菜单返回到应用程序).

The ad loads and is clickable, but is not drawn to the screen. When I leave the activity and return, the ad IS drawn (e.g. by pressing the home button and then returning to the app through the recents menu).

这不是等待广告加载的问题,因为 1) 如果我等待,广告不会显示,以及 2) 广告即使不可见也可以点击.

It's not a matter of waiting for the ad to load, as 1) the ad doesn't display if I wait, and 2) the ad is clickable even though it is not visible.

这是我的布局代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/layout_mainmenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.xxx.menu.SurfaceViewMainMenu
  android:id="@+id/view_mainmenu"
  android:layout_width="match_parent"
  android:layout_height="match_parent"/>


     <com.google.android.gms.ads.AdView
    android:id="@+id/ad"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignTop="@id/view_mainmenu"
    android:layout_alignLeft="@id/view_mainmenu"
    ads:adSize="BANNER"
    ads:adUnitId="@string/ad_unit_id_banner"/>

首次运行 RenderThread 时,加载广告的代码作为 Runnable 由 RenderThread 发布到 UI 线程.

The code that loads the ad is posted as a Runnable to the UI thread by the RenderThread when the RenderThread is first run.

有没有人知道如何让广告立即展示?或者为什么只有在我返回活动时才会绘制广告?

Does anyone have any ideas how I can make the ad show straight away? Or why the ad will only be drawn when I return to the activity?

我试过在 RelativeLayout 上调用 forceLayout()/requestLayout() 但这没有帮助.我还尝试在视图上调用 invalidate().

I've tried calling forceLayout() / requestLayout() on the RelativeLayout but this doesn't help. I also tried calling invalidate() on the view.

谢谢,汤姆.

编辑

我向 AdView 添加了一个 AdListener 并且可以确认正在调用 onAdLoaded.广告已加载但不可见.

I added an AdListener to the AdView and can confirm that onAdLoaded is being called. The ad is loaded but not visible.

什么有效:我在 onAdLoaded 回调中发出另一个 adLoad 请求,然后广告显示.

What works: I make another adLoad request in the onAdLoaded callback, and then the ad shows up.

因此,广告仅在我提出第二个请求后显示.有人知道这里发生了什么吗?

So the ad only shows up after the second request I make. Anyone know what's going on here?

推荐答案

你已经告诉你的 SurfaceView 与 match_parent 高度匹配,所以我对你的 AdView 没有空间感到惊讶.

You have told your SurfaceView to match_parent it's height, so I not surprised that your AdView has no room.

建议您使用 LinearLayout 而不是 RelativeLayout 并让 SurfaceView 使用

Suggest you use a LinearLayout instead of a RelativeLayout and have the SurfaceView use a

layout_height="wrap_content"
layout_width="match_parent"
layout_weight="1" 

这样您的 AdView 就会显示出来,但 SurfaceView 会扩展以填充剩余空间.

so that you AdView is shown, but the SurfaceView expands to fill the remaining space.

这篇关于Android AdMob:addView 在返回活动之前不会显示广告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆