我正在制作Android游戏。我有我的Main类,它扩展了活动,我的Game类扩展了View。
我将Activity的内容视图设置为Game类的实例。所以,我从不使用XML制作的任何布局。
我在向Game对象添加AdMob广告时遇到问题。我不确定如何设置它。这就是我现在所拥有的:
AdView adView;
String AD_ID = "XXXXXXXXXXXXXX";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
game = new Game(this);
setContentView(game);
// Create the adView
adView = new AdView(this, AdSize.BANNER, AD_ID);
LinearLayout layout = (LinearLayout)game; // ERROR HERE
// Add the adView to it
layout.addView(adView);
// Initiate a generic request to load it with an ad
adView.loadAd(new AdRequest());
}
public class Game extends View{
...
}
此外,这是我的清单:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<application
android:icon="@drawable/icon"
android:label="@string/app_name" >
<activity
android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation"
/>
我不确定如何使用LinearLayout并在我的类中展示扩展View的广告。 有什么帮助吗?
答案 0 :(得分:0)
这应该适合你:
AdView adView;
String AD_ID = "XXXXXXXXXXXXXX";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
LinearLayout mainPanel = new LinearLayout(this);
mainPanel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
mainPanel.setOrientation(LinearLayout.VERTICAL);
Game game = new Game(this);
adView = new AdView(this, AdSize.BANNER, AD_ID);
mainPanel.addView(game);
mainPanel.addView(adView);
// Initiate a generic request to load it with an ad
adView.loadAd(new AdRequest());
setContentView(mainPanel);
}
public class Game extends LinearLayout{
...
}