2010-12-10 63 views
2

我设法将adMob添加到标题屏幕,该屏幕使用* .xml文件编写。如何在Android中将AdMob添加到ViewClass中

setContentView(R.layout.main); 
AdView adView = (AdView)findViewById(R.id.ad); 
adView.requestFreshAd(); 

main.xml中包含:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:myapp="http://schemas.android.com/apk/res/de.xazen.tictactoe" 
     android:background="@drawable/title" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     > 
     <com.admob.android.ads.AdView 
       android:id="@+id/ad" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_alignParentBottom="true" 
       myapp:backgroundColor="#000000" 
       myapp:primaryTextColor="#FFFFFF" 
       myapp:secondaryTextColor="#CCCCCC" 
       /> 
</RelativeLayout> 

游戏本身是写在Tttview。

public class TttView extends View 

活动Game.java创建一个TttView的实例并使用setcontent(tttView)将其用作布局。

public class Game extends Activity{ 

    private TttView tttView; 
    . 
    . 
    . 
    setContentView(tttView); 
} 

如何将adMob添加到游戏本身?

回答

0

那么它不是从你的问题很明显就是tttView或者它是如何设置的,但如果它创建了一个布局类型,以显示你可以通过编程添加的AdView到LinearLayout中或类似这样的:

// Setup layout parameters 
    LinearLayout.LayoutParams params; 
    params = new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.FILL_PARENT, 
      LinearLayout.LayoutParams.WRAP_CONTENT); 

    // Create a linear layout 
    LinearLayout layout = new LinearLayout(mContext); 
    layout.setOrientation(LinearLayout.VERTICAL); 
    layout.setPadding(6,6,6,6); 

    AdView mAdView = new AdView(this); 
    layout.addView(mAdView,params); 

或者,如果你是从一个XML文件加载的布局,你可以做类似你所拥有的上述

setContentView(R.layout.game); 

其中game.xml是:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:myapp="http://schemas.android.com/apk/res/de.xazen.tictactoe" 
android:background="@drawable/title" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<tttView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    ... 
    /> 
<com.admob.android.ads.AdView 
     android:id="@+id/ad" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     myapp:backgroundColor="#000000" 
     myapp:primaryTextColor="#FFFFFF" 
     myapp:secondaryTextColor="#CCCCCC" 
     /> 
</RelativeLayout> 
相关问题