Java 在列表视图中包含AdMobs广告

Java 在列表视图中包含AdMobs广告,java,android,admob,Java,Android,Admob,我想在我的应用程序中包括AdMobs广告,但我不知道如何添加它们 我的主要活动在My main.xml上调用setContentView,但My main.xml只包含一个ListView <ListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listView" android:layout_width="match_parent" android:lay

我想在我的应用程序中包括AdMobs广告,但我不知道如何添加它们

我的主要活动在My main.xml上调用setContentView,但My main.xml只包含一个ListView

<ListView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/listView"
  android:layout_width="match_parent" 
  android:layout_height="wrap_content"
  android:drawSelectorOnTop="true"
  />

将adview添加到布局中

<ListView
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:myapp="http://schemas.android.com/apk/res/com.t3hh4xx0r.romcrawler"
  android:id="@+id/listView"
  android:layout_width="match_parent" 
  android:layout_height="wrap_content"
  android:drawSelectorOnTop="true" >

  <com.google.ads.AdView 
    android:id="@+id/ad"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    myapp:backgroundColor="#000000" 
    myapp:primaryTextColor="#FFFFFF" 
    myapp:secondaryTextColor="#CCCCCC" />
  </ListView>

当然,在启动时会导致强制关闭,因为ListView不允许有子布局

最好的办法是什么?用java创建广告视图?如果是这样,你能提供一些我将如何创建它的示例代码吗


提前谢谢你

创建如下布局:这样广告将显示在底部。通过将权重和设置为1,然后说listview的权重为1,它将占用创建广告后剩余的所有空间

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="1" >

    <ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="#E4E9F5"
    android:cacheColorHint="#00000000" />

    <com.google.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/adView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    ads:adSize="BANNER"
    ads:adUnitId="your_adunitid_here"
    ads:loadAdOnCreate="true" />

</LinearLayout>

创建如下布局:这样广告将显示在底部。通过将权重和设置为1,然后说listview的权重为1,它将占用创建广告后剩余的所有空间

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="1" >

    <ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="#E4E9F5"
    android:cacheColorHint="#00000000" />

    <com.google.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/adView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    ads:adSize="BANNER"
    ads:adUnitId="your_adunitid_here"
    ads:loadAdOnCreate="true" />

</LinearLayout>


让我走上了正确的轨道,但我仍然没有实际显示的广告。在logcat中,我看到它接收到了广告url,但没有看到广告本身<代码>I/Ads(1432):onFailedToReceiveAd(广告请求成功,但由于缺少广告资源清册,没有返回广告。)是什么原因造成的?好的,您是否填写了adUnitId并在清单中包含INTERNET权限?即使我的设备被设置为测试设备,我仍然会收到相同的消息<代码>AdRequest ar=新AdRequest();ar.addTestDevice(“4B605421C6EAA120E18B7FA165D2E980”)adUnitID是AdMob提供的“发布者ID”。当您在AdMob中查看应用程序并单击“管理设置”时,您会发现这一点。还有internet权限?让我走上了正确的轨道,但我仍然没有实际显示的广告。在logcat中,我看到它接收到了广告url,但没有看到广告本身<代码>I/Ads(1432):onFailedToReceiveAd(广告请求成功,但由于缺少广告资源清册,没有返回广告。)是什么原因造成的?好的,您是否填写了adUnitId并在清单中包含INTERNET权限?即使我的设备被设置为测试设备,我仍然会收到相同的消息<代码>AdRequest ar=新AdRequest();ar.addTestDevice(“4B605421C6EAA120E18B7FA165D2E980”)adUnitID是AdMob提供的“发布者ID”。当您在AdMob中查看应用程序并单击“管理设置”时,您会发现这一点。是否还有internet权限?