我遇到的问题是我无法正确显示带有两个广告横幅的 GLSurfaceView。我想将它们放置在移动屏幕的顶部和底部以及 GLSurfaceView(主游戏窗口)上方。如何实现?下面是我当前的布局 xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/adRelativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray" >
<ad banner 1
android:id="@+id/admobView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
<ad banner 2
android:id="@+id/mmediaView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
<android.opengl.GLSurfaceView
android:id="@+id/gameWindow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
最佳答案
我以前也遇到过这个问题。您可以做的一件事是在 Activity 的 onCreate
方法中动态创建一个 RelativeLayout
。在此布局中,您将正常实例化 GLSurfaceView,但不是使用 setContentView
将其设置为 View ,而是将其作为 View 添加到布局中。您还可以根据需要将横幅添加到 View ,然后将布局设置为 View ,而不仅仅是 GLSurfaceView。下面是我用来在游戏 View 底部添加横幅的方法。看看您是否可以根据自己的需要对其进行调整。
RelativeLayout layout = new RelativeLayout(this);
int adId = 0x12346;
AdView adView = new AdView(this, AdSize.BANNER, "YOUR_AD_ID_NUMBER");
adView.setId(adId);
GLSurfaceView glView = new GLSurfaceView(this);
RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
RelativeLayout.LayoutParams glParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
glParams.addRule(RelativeLayout.ABOVE, adId);
glParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
layout.addView(adView, adParams);
layout.addView(glView, glParams);
setContentView(layout);
adView.loadAd(new AdRequest());
关于android - 带有广告横幅的相对布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16107157/