2012-05-22 33 views
0

我在游戏中实现了admob,但我的游戏使用surfaceview来显示图形。显示隐藏在Surfaceview中的AdMob

我该如何“访问”表面视图内的AdMob视图?

编辑2:

试图执行回调:

MainActivity.class

interface AdMobInterface { 
public void HideAd(); 
public void ShowAd(); 
} 

public class MainActivity extends Activity implements AdListener { 

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

    layout = new RelativeLayout(this); 
    engine = new Engine(this); 
    layout.addView(engine); 

    if(engine.IsDemoVersion) { 
     SetupAdMob(); 
    } 

    setContentView(layout); 

} 


public void ShowAd() { 
     ///Execute this from Engine.class 
} 

public void HideAd() { 
      ///Execute this from Engine.class 
} 

private void SetupAdMob() { 
    String AdMobPublisherID = "XXXXXXXXXX"; 
    adView = new AdView(this, AdSize.BANNER, AdMobPublisherID); 
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
    layout.addView(adView, params); 
    layout.bringChildToFront(layout.getChildAt(1)); 
    adView.loadAd(new AdRequest()); 
} 


} 

Engine.class

public class Engine extends SurfaceView implements 
    SurfaceHolder.Callback, SensorEventListener, AdMobInterface { 

AdMobInterface AdMob; 

    public Engine(Context context, AdMobInterface admob) { 
    super(context); 


    AdMob = admob; 

} 


} 

回答

0

使用FrameLayout并在表面视图顶部显示添加。像这样:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/root" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <SurfaceView android:layout_width="fill_parent" 
       android:layout_height="fill_parent" /> 
    <AdView id="@+id/ad" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" /> 
</FrameLayout> 

然后,你需要做的就是通过ID找到AdView并从父布局中删除它。

View adView = findViewById(R.id.ad); 
FrameLayout root = (FrameLayout)findViewById(R.id.root); 
root.removeView(adView); 

最好从活动内部做到这一点,不需要你的'引擎'来了解广告或布局。

+0

但游戏发生在'引擎'里面 - 我不能点击屏幕上的“按钮”对象来切换广告? this.ContentViewContainer.HideAd(); ...类似的东西 – GideonKain

+0

将回调接口传递给您的引擎,并在需要时调用它。您的基本活动将执行界面并执行实际的添加/删除操作。 –

+0

有关如何“传递CallBack接口”的任何提示? 发现此:http://www.javaworld.com/javatips/jw-javatip10.html 试图执行它无济于事 – GideonKain