【问题标题】:Android Admob ad not shown in Activity with fragment. Why?带有片段的 Activity 中未显示 Android Admob 广告。为什么?
【发布时间】:2015-09-28 05:56:37
【问题描述】:

我有一个活动如下。 我使用setContentView(R.layout.below); 在另一个活动中启动它。但是,below.xml包含一个寻呼机(这是一个片段,有自己的文本视图、网络视图等)。我的问题是即使 Google Ads 块显示在 Eclipse 的图形布局中,并且当我运行下面显示的 java 代码时没有抛出错误;我看不到广告。我不明白发生了什么!我尝试了很多东西,包括将此布局更改为简单的相对布局。以下是我如何称呼这个 adView。

    setContentView(R.layout.below);
    AdView mAdView2 = (AdView) findViewById(R.id.adView2);
    AdRequest adRequest2 = new     
    AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
            mAdView2.loadAd(adRequest2);

这是包含此 adView 的实际 XML 文件。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="5dp"
    android:paddingRight="5dp" >

    <LinearLayout
        android:id="@+id/home_layout"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_above="@+id/footerLayout">
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </android.support.v4.view.ViewPager>

    </LinearLayout>
    <LinearLayout
            android:id="@+id/footerLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="bottom"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"
            >
    <com.google.android.gms.ads.AdView
        android:id="@+id/adView2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="0dp"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="@string/banner_ad_unit_id" >
    </com.google.android.gms.ads.AdView>
    </LinearLayout>


</RelativeLayout>

【问题讨论】:

  • fill_parent 已弃用。更喜欢 match_parent

标签: android android-fragments admob android-relativelayout adview


【解决方案1】:

尝试删除RelativeLayout的填充,也发生在我的应用程序中,但删除填充后它可以工作

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="5dp"   <--- REMOVE
    android:paddingRight="5dp" > <--- REMOVE

如果您必须使用填充,请将其添加到 adview 的父布局中,这样它将在 LinearLayout 上

【讨论】:

  • 奇怪的是,这行得通。谢谢。你知道为什么会这样吗?另外,如果可以的话,请检查我的其他问题:stackoverflow.com/questions/32810527/…
  • adview 显然不允许填充,这可能是谷歌设置的那样......
  • 是的,问题在于父布局的填充。谢谢
【解决方案2】:

给广告视图一些高度它的高度取决于设备屏幕尺寸它可以是 50dp,120dp

如果没有任何反应就使用这个,然后请换个尺寸再试一次

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:id="@+id/home_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_above="@+id/footerLayout">
<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
</android.support.v4.view.ViewPager>

</LinearLayout>
<LinearLayout
        android:id="@+id/footerLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"

        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        >
<com.google.android.gms.ads.AdView
    android:id="@+id/adView2"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:layout_marginBottom="0dp"
    ads:adSize="SMART_BANNER"
    ads:adUnitId="@string/banner_ad_unit_id" >
</com.google.android.gms.ads.AdView>
</LinearLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多