Android Admob ad not shown in Activity with fragment. Why?

做~自己de王妃 提交于 2019-12-25 03:36:23

问题


I have an activity as follows. I launch it inside another activity using setContentView(R.layout.below); However, below.xmlcontains a pager (which is a fragment and has its own textview, webview etc.). My problem is even though the Google Ads block is shown in the graphical layout on Eclipse and NO error is thrown when I run the java code shown below; I CAN NOT see the ad. I don't understand what is happening! I've tried so many things, including changing this layout to a simple relative layout. The following is how I call this 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);

This is the actual XML file that contains this adView.

<?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>

回答1:


try removing the padding of RelativeLayout, also happens to my app, but got it working after removing the padding

<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

if you must use a padding, add it on parent layout of the adview, so it will be on LinearLayout




回答2:


Give some heights to ad view it's height depends on device screen size it can be 50dp,120dp

use this if nothing happens then please put different size and try one more time

<?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>



来源:https://stackoverflow.com/questions/32816488/android-admob-ad-not-shown-in-activity-with-fragment-why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!