片段中的Adview未显示

时间:2015-07-01 08:40:14

标签: android android-fragments adsense adview

您好我正在尝试在片段中显示广告,但我没有得到任何结果,它只显示空白区域,在活动中它工作正常,

对于片段,我编写了如下代码:

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@drawable/innerbg"
  xmlns:app="http://schemas.android.com/apk/lib/com.google.ads">

<ListView
    android:id="@+id/listview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@null"
    android:listSelector="@android:color/transparent"
    android:layout_above="@+id/adView"
    android:layout_alignParentTop="true"/>

 <com.google.android.gms.ads.AdView 
           xmlns:ads="http://schemas.android.com/apk/res-auto"
           android:id="@+id/adView"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           ads:adSize="BANNER"
           ads:adUnitId="@string/adunit_id_home"
           android:layout_alignParentBottom="true"/>

</RelativeLayout>   

在片段中:

AdView adViewHome = (AdView) rootView.findViewById(R.id.adViewHome);
AdRequest adRequest = new AdRequest.Builder().build();
adViewHome.loadAd(adRequest);

1 个答案:

答案 0 :(得分:0)

您的列表视图占据整个屏幕,因此广告视图无法展示。将您的布​​局更改为此。

<RelativeLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ListView
        android:choiceMode="singleChoice"
        android:divider="@null"
        android:id="@+id/listview"
        android:layout_alignParentTop="true"
        android:layout_gravity="start"
        android:layout_width="match_parent"
        android:listSelector="@android:color/transparent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/adView" />

    <com.google.android.gms.ads.AdView     
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_alignParentBottom="true"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        ads:loadAdOnCreate="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/adunit_id_home" />
</RelativeLayout>

在片段中:

AdView adViewHome = (AdView) rootView.findViewById(R.id.adViewHome);
AdRequest adRequest = new AdRequest.Builder().build();
adRequest.setTesting(true);
adViewHome.loadAd(adRequest);