2010-12-10 6 views
2

Je parviens à ajouter adMob à l'écran de titre, qui est écrit dans un fichier * .xml.Comment ajouter AdMob à ViewClass dans Android

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

main.xml contient:

<?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> 

Le jeu lui-même est écrit dans Tttview.

public class TttView extends View 

L'activité Game.java créer une instance de TttView et utilise setContent (tttView) pour l'utiliser comme mise en page.

public class Game extends Activity{ 

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

Comment puis-je ajouter adMob au jeu lui-même?

Répondre

0

Eh bien, il est pas évident de votre question ce tttView est ou comment il est configuré, mais si elle crée un type de mise en page pour afficher vous pouvez ajouter un AdView programatically dans un LinearLayout ou similaire comme celui-ci:

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

ou si vous chargez la mise en page à partir d'un fichier XML que vous pouvez faire ce que vous avez même au-dessus

setContentView(R.layout.game); 

Où Game.xml est:

<?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> 
Questions connexes