问题
I have an activity in form of a small game which uses SurfaceView. Below are the code snippets.. I am confused how to implement admob on surfaceview. Please suggest.
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 {
}
Answer
Updated the code as below
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();
}
回答1:
Have the root view for DroidzActivity be a LinearLayout containging an AdView and MainGamePanel.
来源:https://stackoverflow.com/questions/15918847/android-implement-admob-on-surfaceview