画布上的android admob

画布上的android admob,android,android-canvas,surfaceview,Android,Android Canvas,Surfaceview,我正在尝试将admob添加到我的游戏中, 我正在使用surfaceview绘制画布, 这就是主要的布局 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="ma

我正在尝试将admob添加到我的游戏中, 我正在使用surfaceview绘制画布, 这就是主要的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <SurfaceView
        android:id="@+id/surface"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true" />

    <com.google.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
        android:id="@+id/ad"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        ads:adSize="BANNER"
        ads:adUnitId="a151e48ea219c98"
        android:visibility="visible" />
</RelativeLayout>
这是我在曲面上运行绘图画布的类

public class MySurface extends SurfaceView implements Runnable {
            .....
        @Override
        public void run() {
            // TODO Auto-generated method stub

            while (isRunning) {
                canvas.drawBitmap(bg, 0, 0, null);
        }
            .....
}

问题是我无法将我们的surfaceview与布局上的surfaceview“连接”起来,以使用它进行绘图

您永远不会实例化MySurface

修改activity_main.xml以显式实例化它:

<com.yourcompany.MySurface
        android:id="@+id/surface"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true" />

或者通过编程将其添加到主要活动的创建中

还要注意,指定SurfaceView的布局宽度和高度以匹配父视图将意味着AdView将永远不会有任何空间。首先指定AdView并将SurfaceView标记为AdView下方,或者使用LinearLayout而不是RelativeLayout,并对SurfaceView使用layout_weight=1,以使其展开以填充未使用的空间

<com.yourcompany.MySurface
        android:id="@+id/surface"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true" />