0

我正在制作一款安卓游戏。我有我的 Main 类,它扩展了活动,我的 Game 类扩展了 View。

我将 Activity 的内容视图设置为 Game 类的实例。所以,我从不使用任何用 XML 制作的布局。

我在将 AdMob 广告添加到 Game 对象时遇到问题。我不确定如何设置它。这就是我现在所拥有的:

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 的类中显示广告。有什么帮助吗?

4

1 回答 1

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{
...
}
于 2012-06-04T02:34:12.923 回答