3

最后,我的GLSurfaceview顶部有一个横幅广告。但是,它显示为黑色背景,占据了整个屏幕的宽度,并像这样覆盖了我的游戏区域的顶部(我还应该指出,游戏区域也向下移动了一点,所以底部也不见了) .

在此处输入图像描述

我需要做的是将横幅移动到屏幕底部,并将其保持在中央并删除此黑色背景,使其看起来像这样:

在此处输入图像描述

我尝试使用 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);

}

当横幅更改时,它也会出现“闪烁”,但我可以在单独的问题中处理。

4

2 回答 2

4

Use a RelativeLayout or a FrameLayout as your parent layout, then just define the layout parameters for the adView to be positioned at the bottom center of the screen like this. Below is a solution:

 @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);

}
于 2014-04-15T00:42:15.873 回答
0

当您将它们都添加到线性布局(表面视图和广告视图)时,您有两个独立的视图。我认为您想要完成的是将广告覆盖在您的表面视图之上。这个页面很旧,但请查看http://www.curious-creature.org/2009/03/01/android-layout-tricks-3-optimize-part-1/以获取有关如何执行此操作的示例。

于 2014-04-15T00:24:55.337 回答