Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.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
Java 如何在不使用xml的情况下向游戏中添加广告?_Java_Android_Xml_Android Activity_Admob - Fatal编程技术网

Java 如何在不使用xml的情况下向游戏中添加广告?

Java 如何在不使用xml的情况下向游戏中添加广告?,java,android,xml,android-activity,admob,Java,Android,Xml,Android Activity,Admob,我想在我的游戏中添加广告,但我不使用xml。我知道我应该学习,但是,这是我的第一个游戏,我只是想先发布它。这就是我目前所拥有的。我也一直跟踪到说编辑main_activity.xml的部分。我现在应该如何添加广告 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //fullscreen requestWindowFeatur

我想在我的游戏中添加广告,但我不使用xml。我知道我应该学习,但是,这是我的第一个游戏,我只是想先发布它。这就是我目前所拥有的。我也一直跟踪到说编辑main_activity.xml的部分。我现在应该如何添加广告

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //fullscreen
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.Flag_FULLSCREEN);

    //scale image
    Display display = getWindowManager().getDefaultDisplay();
    size = new Point();
    dispay.getSize(size);
    dispX = size.x;
    dispY = size.y;
    bg = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(
            getResources(), R.drawable.bg),dispX,dispY,true);
    bg = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(
            getResources(), R.drawable.losescreen),GamePanel.rescaleX(805),GamePanelrescaleY(1105),true);
    bg = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(
            getResources(), R.drawable.playbutton),GamePanel.rescaleX(242),GamePanelrescaleY(242),true);

    setContentView(new GamePanel(this));
    contextOfApplication = getApplicationContext();
}

此代码替代了在xml中添加adview:

AdView adView = new AdView(this, AdSize.BANNER, MY_AD_UNIT_ID);
// Lookup the layout where you want to place the ad unit
// give the layout an id in the layout xml
LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);
// Add the adView to the activity's layout
layout.addView(adView);
以下代码的其余部分请参见教程