【问题标题】:How do I add ads to my game without using xml?如何在不使用 xml 的情况下将广告添加到我的游戏中?
【发布时间】:2015-12-19 05:50:09
【问题描述】:

我想在我的游戏中添加广告,但我没有使用 xml。我知道我应该学习,但是这是我的第一款游戏,我只想先发布它。这就是我到目前为止所拥有的。我也一直关注Admob tutorial,直到说编辑 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();
}

【问题讨论】:

    标签: java android xml android-activity admob


    【解决方案1】:

    此代码是在 xml 中添加广告视图的替代:

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

    下面的其余代码按照教程。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多