2015-09-14 21 views
0

我很努力在我的应用中实现admob横幅,因为setContentView()方法用于名为gameView的surfaceView,因此在xml中创建adView无法应用于此框架作为setContentView已被使用。我不知道如何以编程方式执行此操作。有没有人有解决这个问题?当setContentView()用于Surfaceview时实现AdMob横幅

我的主要活动:

public class GameMainActivity extends BaseGameActivity { 
.... 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    instance = this; 
    prefs = getPreferences(Activity.MODE_PRIVATE); // New line! 
    highScore = retrieveHighScore(); 
    highScoreUnits = retrieveHighScoreUnits(); 
    highScoreTens = retrieveHighScoreTens(); 
    highScoreHundreds = retrieveHighScoreHundreds(); 
    muteButton = retrieveMuteButton(); 
    assets = getAssets(); 
    sGame = new GameView(this, GAME_WIDTH, GAME_HEIGHT); 
    setContentView(sGame); 
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 

} 

和我的自定义surfaceView代码

public class GameView extends SurfaceView implements Runnable { 

private Bitmap gameImage; 
private Rect gameImageSrc; 
private Rect gameImageDst; 
private Canvas gameCanvas; 
private Painter graphics; 

private Thread gameThread; 
private volatile boolean running = false; 
private volatile State currentState; 

private InputHandler inputHandler; 


public GameView(Context context, int gameWidth, int gameHeight) { 
    super(context); 
    gameImage = Bitmap.createBitmap(gameWidth, gameHeight, 
      Bitmap.Config.RGB_565); 
    gameImageSrc = new Rect(0, 0, gameImage.getWidth(), 
      gameImage.getHeight()); 
    gameImageDst = new Rect(); 
    gameCanvas = new Canvas(gameImage); 
    graphics = new Painter(gameCanvas); 
    SurfaceHolder holder = getHolder(); 
    holder.addCallback(new Callback() { 

     @Override 
     public void surfaceCreated(SurfaceHolder holder) { 
      initInput(); 
      if (currentState == null) { 
       setCurrentState(new LoadState()); 
      } 
      initGame(); 
     } 

     @Override 
     public void surfaceChanged(SurfaceHolder holder, int format, 
            int width, int height) { 
      // TODO Auto-generated method stub 
     } 

     @Override 
     public void surfaceDestroyed(SurfaceHolder holder) { 
      pauseGame(); 
     } 

    }); 
} 
+0

你试过了吗?addContent用AdView查看? –

回答

1

使用的RelativeLayout或FrameLayout里为你的父母的布局,那么就定义布局参数AdView的被定位(例如在这样的屏幕底部中心):

public class GameMainActivity extends BaseGameActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     instance = this; 
     prefs = getPreferences(Activity.MODE_PRIVATE); // New line! 
     highScore = retrieveHighScore(); 
     highScoreUnits = retrieveHighScoreUnits(); 
     highScoreTens = retrieveHighScoreTens(); 
     highScoreHundreds = retrieveHighScoreHundreds(); 
     muteButton = retrieveMuteButton(); 
     assets = getAssets(); 
     // Create an ad. 
     AdView adView = new AdView(this); 
     adView.setAdSize(AdSize.BANNER); 
     adView.setAdUnitId(AD_UNIT_ID); 
     // set background color of adview to force it to show 
     adView.setBackgroundColor(Color.TRANSPARENT); 
     // Add the AdView to the view hierarchy. The view will have no size 
     // until the ad is loaded. 
     RelativeLayout layout = new RelativeLayout(this); 
     layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 
       LayoutParams.MATCH_PARENT)); 
     // Create an ad request. 
     AdRequest adRequest = new AdRequest.Builder().build(); 

     // Start loading the ad in the background. 
     adView.loadAd(adRequest); 

     // Request full screen 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); 

     // Create and set your game's view 
     sGame = new GameView(this, GAME_WIDTH, GAME_HEIGHT); 


     RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(
     RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 

     adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
     adParams.addRule(RelativeLayout.CENTER_HORIZONTAL); 
     layout.addView(sGame); 
     layout.addView(adView, adParams); 
     setContentView(layout); 
     getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
    } 
}