AdView 不显示 ExpandableListView

AdView nots showing with ExpandableListView

我想将 Admob 广告与我的 android 应用程序集成。一切正常,但广告横幅未显示!我不知道是什么问题。 任何帮助请 这是我的代码。 对于 activity_main.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#f4f4f4"
android:orientation="vertical" >

<ExpandableListView
    android:id="@+id/lvExp"
     android:layout_width="match_parent"
     android:layout_height="wrap_content" />

<com.google.ads.AdView
    android:id="@+id/adView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    ads:adSize="BANNER"
    ads:adUnitId="ca-app-pub-7203358988240052/2007826925"
    ads:loadAdOnCreate="true"/>

</LinearLayout>

对于我的 Main_Activity.java 我使用了 :

    AdView adView = (AdView)this.findViewById(R.id.adView);
    adView.loadAd(new AdRequest());

您正在使用采用 "eldest child first" 方法的 LinearLayout。在您的布局中,您的老大是 ExpandableListView (ELV),尽管您已指定 ELV 的高度应包裹其内容,但它仍占据全屏 space,因为ELV 做到这一点。因此,您的 AdsView 没有 space 显示。

一种选择是使用 android:layout_weight 属性在不同的小部件之间分配可用屏幕 space。因此,如果您希望小部件共享屏幕 space,使 ELV 获得 90% 的屏幕而 AdsView 获得 10% 的屏幕,您可以这样做:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#f4f4f4"
android:orientation="vertical" >

<ExpandableListView
    android:id="@+id/lvExp"
     android:layout_width="match_parent"
     android:layout_height="0dip" <!-- When you are using weights, set the height to 0 as the weight will control the height -->
     android:layout_weight="90" />

<com.google.ads.AdView
    android:id="@+id/adView"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="10"
    ads:adSize="BANNER"
    ads:adUnitId="ca-app-pub-7203358988240052/2007826925"
    ads:loadAdOnCreate="true"/>

</LinearLayout>

当您使用权重时,您应该将高度设置为零,因为权重将控制垂直方向的高度。

随便使用你想要的比例。您可以使用 80/20 或 75/25 或其他。

编辑: 我从您提供的 link 和 运行 代码下载了您的应用程序。我注意到的第一件事是您的 AdView 确实占用了 space。这意味着布局很好 - 您没有收到任何要展示的广告。因此,我查看了您的代码一段时间后,立即意识到您使用的是旧版 Google 移动广告 SDK。我知道这已被弃用(请参阅 https://developers.google.com/mobile-ads-sdk/android-legacy/),这意味着您无法使用它向 Play 商店提交应用。它只会被拒绝。但我不确定这是否是原因。我没有收到您收到的错误消息。相反,我收到了:

No AdResponse found, <message: /log> 
Bad request.
onFailedToReceiveAd(Invalid Ad request.)

这意味着您正在从服务器获取一些东西。所以你的请求是正确的。因此,我做了一些研究,发现前几次没有返回广告是很常见的(本论坛中有相当多的答案说明了这一点),这个问题似乎存在于旧版 SDK 中.所以这可能是原因。除此之外,我想不出别的了。我有一些理论,但在我确定之前我需要做一些研究。此外,在旁注中,您需要:

ads:testDevices="your device id goes here" // one you get when you type adb devices
// this is to ensure that the services know you are simply testing and not using your own account to hit the service

总而言之,我建议您迁移到 Google Play 服务 Admob。我之前只将它用于练习和学习目的,并且效果很好。希望这有帮助。