Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 当setContentView()用于Surfaceview时实现Admob横幅_Android_Admob_Surfaceview_Banner Ads_Setcontentview - Fatal编程技术网

Android 当setContentView()用于Surfaceview时实现Admob横幅

Android 当setContentView()用于Surfaceview时实现Admob横幅,android,admob,surfaceview,banner-ads,setcontentview,Android,Admob,Surfaceview,Banner Ads,Setcontentview,我正在努力在我的应用程序中实现admob横幅,因为setContentView()方法用于名为gameView的surfaceView,因此无法将创建xml格式的adView应用于此框架,因为setContentView已经在使用。我不知道如何通过编程来实现这一点。有人能解决这个问题吗 我的主要活动: public class GameMainActivity extends BaseGameActivity { .... @Override protected void onCreate(B

我正在努力在我的应用程序中实现admob横幅,因为setContentView()方法用于名为gameView的surfaceView,因此无法将创建xml格式的adView应用于此框架,因为setContentView已经在使用。我不知道如何通过编程来实现这一点。有人能解决这个问题吗

我的主要活动:

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();
        }

    });
}

使用RelativeLayout或FrameLayout作为父布局,然后仅定义要定位的adView的布局参数(例如,在屏幕的底部中心,如下所示):


您是否尝试过使用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); 
    }
}