2013-04-10 86 views
3

我有一個使用SurfaceView的小遊戲形式的活動。下面是代碼片段..我很困惑如何在SurfaceView上實現admob。請建議。Android:在surfaceview上實現admob

public class DroidzActivity extends Activity { 

    private static final String TAG = DroidzActivity.class.getSimpleName(); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // requesting to turn the title OFF 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     // making it full screen 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     // set our MainGamePanel as the View 
     setContentView(new MainGamePanel(this)); 
     Log.d(TAG, "View added"); 
    } 
} 

public class MainGamePanel extends SurfaceView implements 
     SurfaceHolder.Callback { 
} 

回答

更新了代碼如下

public class DroidzActivity extends Activity { 
    /** Called when the activity is first created. */ 

    private static final String TAG = DroidzActivity.class.getSimpleName(); 
    private static final String MY_AD_UNIT_ID = "XYZ"; 
    private AdView adView; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // requesting to turn the title OFF 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     // making it full screen 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     // set our MainGamePanel as the View 
     //setContentView(new MainGamePanel(this)); 

     adView = new AdView(this, AdSize.SMART_BANNER, MY_AD_UNIT_ID); 
     RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.WRAP_CONTENT, 
       RelativeLayout.LayoutParams.WRAP_CONTENT); 
     lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 

     adView.setLayoutParams(lp); 

     RelativeLayout layout = new RelativeLayout(this); 
     layout.addView(new MainGamePanel(this)); 
     layout.addView(adView); 
     adView.loadAd(new AdRequest()); 
     setContentView(layout); 

     Log.d(TAG, "View added"); 
    } 

    @Override 
    protected void onDestroy() { 
     Log.d(TAG, "Destroying..."); 
     if (adView != null) { 
       adView.destroy(); 
      }  
     super.onDestroy(); 
    } 

回答

2

具有用於DroidzActivity根視圖是一個的LinearLayout containging一個的AdView和MainGamePanel。

+0

Thansk提示威廉,更新了問題的答案。請查閱! – RDX 2013-04-14 09:21:16

+0

就我個人而言,我已經在XML中定義了佈局,但是如果你願意,你可以編寫它。 – William 2013-04-15 11:37:32