Android: how to display admob ads on GLSurfaceView

巧了我就是萌 提交于 2019-12-07 14:31:39

问题


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.



来源:https://stackoverflow.com/questions/12689484/android-how-to-display-admob-ads-on-glsurfaceview

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