我通过Android中的drawable canvas实现游戏,我想要横幅广告,但无法显示。这是代码: -
GameActivity.java(此GameActivity.java连接到我的gameview.java文件,我在可绘制画布的帮助下使用所有游戏代码)
public class GameActivity extends Activity
{
private AdView mAdView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//GameView gView = new GameView(this);
GameView gView = new GameView(this);
gView.setKeepScreenOn(true);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
MobileAds.initialize(getApplicationContext(), "ca-app-pub-0664573200302260/332653322424");
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
//Additionally to adjust the position to Bottom
layout.setGravity(Gravity.BOTTOM);
// Create a banner ad
mAdView = new AdView(this);
mAdView.setVisibility(View.VISIBLE);
mAdView.setAdSize(AdSize.BANNER);
mAdView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
// Create an ad request.
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
// Optionally populate the ad request builder.
adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
// Add the AdView to the view hierarchy.
layout.addView(mAdView);
// Start loading the ad.
mAdView.loadAd(adRequestBuilder.build());
setContentView(gView);
}
}
答案 0 :(得分:1)
您将内容视图设置为GameView。而是将其设置为布局:
setContentView(layout);
GameView是一个单一视图,而布局包含GameView和AdView