AdMob横幅和GLSurfaceview(以编程方式)

时间:2014-04-15 00:08:28

标签: android opengl-es admob glsurfaceview banner-ads

最后,我在 GLSurfaceview 的顶部有一个横幅广告。然而,它出现在黑色背景中占据屏幕的整个宽度并覆盖我的游戏区域的顶部(我还应该指出游戏区域也向下移动了一点,所以底部也缺失)

enter image description here

我需要做的是将横幅移到屏幕底部,并将其保持在中央并移除此黑色背景,使其如下所示:

enter image description here

我尝试使用XML但是遇到了很多错误所以我转而完全用Java做这个(并且设法得到这么远) - 但是关于如何使用GLSurfaceView执行此操作的质量信息缺少恕我直言,所以我是希望有人能告诉我哪里出错了。

代码

这是我的onCreate()方法:

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

        // Create an ad.
        adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId(AD_UNIT_ID);

        // Add the AdView to the view hierarchy. The view will have no size
        // until the ad is loaded.
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);

        // Create an ad request.
        // get test ads on a physical device.
        AdRequest adRequest = new AdRequest.Builder()
          .addTestDevice(TestDeviceID)
          .build();

        // Start loading the ad in the background.
        adView.loadAd(adRequest);

        //Request full screen
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

        //Create a displayMetrics object to get pixel width and height
        metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        width = metrics.widthPixels;
        height = metrics.heightPixels;

        //Create and set GL view (OpenGL View)
        myView = new MyGLSurfaceView(MainActivity.this);
        layout.addView(adView);
        layout.addView(myView);


        //Create a copy of the Bundle
        if (savedInstanceState != null){
            newBundle = new Bundle(savedInstanceState);         
        }

        //Set main renderer             
        setContentView(layout);

}

当横幅更改时,它也会显示为“闪烁”,但我可以在另外一个问题中处理。

2 个答案:

答案 0 :(得分:3)

使用RelativeLayout或FrameLayout作为父布局,然后只需将adView的布局参数定义为屏幕的底部中心,就像这样。以下是解决方案:

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

        // Create an ad.
        adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId(AD_UNIT_ID);
        adView.setBackgroundColor(Color.TRANSPARENT); 
        // Add the AdView to the view hierarchy. The view will have no size
        // until the ad is loaded.
        RelativeLayout layout = new RelativeLayout(this);
        layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,    LayoutParams.MATCH_PARENT));
        // Create an ad request.
        // get test ads on a physical device.
        AdRequest adRequest = new AdRequest.Builder()
          .addTestDevice(TestDeviceID)
          .build();

        // Start loading the ad in the background.
        adView.loadAd(adRequest);

        //Request full screen
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

        //Create a displayMetrics object to get pixel width and height
        metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        width = metrics.widthPixels;
        height = metrics.heightPixels;

        //Create and set GL view (OpenGL View)
        myView = new MyGLSurfaceView(MainActivity.this);
     RelativeLayout.LayoutParams adParams = 
                new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
            adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);

        layout.addView(myView);
        layout.addView(adView, adParams);


        //Create a copy of the Bundle
        if (savedInstanceState != null){
            newBundle = new Bundle(savedInstanceState);         
        }

        //Set main renderer             
        setContentView(layout);

}

答案 1 :(得分:0)

当您将两个视图添加到线性布局(曲面视图和广告视图)时,您有两个单独的视图。我认为您想要实现的是将广告叠加在表面视图的顶部。此页面已过时但请查看http://www.curious-creature.org/2009/03/01/android-layout-tricks-3-optimize-part-1/以获取有关如何执行此操作的示例。