Android: how to display admob ads on GLSurfaceView

2019-05-14 14:17发布

问题:

I am currently able to get the admob ads on ui view (in separate test project ), but i want to display this ad in GLSurfaceView . I tried to load the ad in onCreate( ) method of activity and in present method of my screen (where all rendering is done) i called

MyGameActivity.mAdView.bringToFront(); //thought it will bring the ad in front of all the game objects.

now on running the project i can see the message in logcat window Recieved ad url "big url" but i cant see the ad on screen. In my game there is only one activity and many game screens. please help me figure out how to display ads on my game screen.

回答1:

You should modify your layout to be something like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layoutMain"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >       
    <RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/layout1"
        android:tag="trueLayout"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        >       
    </RelativeLayout>
</LinearLayout>

This is my code, which is self explanatory.:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);        

    setContentView(R.layout.main);

    LinearLayout layoutMain = (LinearLayout) findViewById(R.id.layoutMain);

    // Create the adView
    // Please replace MY_BANNER_UNIT_ID with your AdMob Publisher ID
    adView = new AdView(this, AdSize.BANNER, "YourPersonalID#"); 

    layoutMain.addView(adView);

    // Initiate a generic request to load it with an ad
    AdRequest request = new AdRequest();
    request.setTesting(true);

    adView.loadAd(request);     

    RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.layout1);
    layout1.setOnTouchListener(this);

    mTestHarness = new GLSurfaceView(this);
    mTestHarness.setEGLConfigChooser(false);
    mTestHarness.setRenderer(this);
        mTestHarness.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);    

    layout1.addView(mTestHarness);
}

After you get this right you will get the equivalent to the BannerEssentials tutorial app from the google play tutorials, but using GLSurfaceView instead.