【问题标题】:Adview in Fragment is not displaying片段中的 Adview 未显示
【发布时间】:2015-07-01 17:14:29
【问题描述】:

您好,我正在尝试在片段中显示 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);

【问题讨论】:

  • 为什么它被否决了?我可以知道原因吗?
  • 我不知道为什么这些人对这个问题投了反对票,如果有任何问题,请告诉我,以便以后不再重复。不通知投反对票是不对的,必须纠正新手

标签: android android-fragments adsense adview


【解决方案1】:

您的列表视图占据了整个屏幕,因此广告视图没有机会显示。把你的布局改成这样。

<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);

【讨论】:

  • 您是否尝试将列表视图设置为包装内容?
  • 是的,我将权限设置为清单并将列表视图高度设置为包装内容
  • 我做了一个编辑,我最后的猜测,将 testing 设置为 true 以在你的 xml 中进行调试和广告:loadAdOnCreate="true"
  • adRequest.setTesting(true);给出错误:“AdRequest 类型的方法 setTesting(boolean) 未定义”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-02
  • 2014-05-27
  • 1970-01-01
  • 2012-08-18
  • 1970-01-01
相关资源
最近更新 更多