我有一个使用 SurfaceView 的小游戏形式的活动。下面是代码片段。我很困惑如何在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();
}