如何在Android中将AdMob添加到ViewClass

时间:2010-12-10 12:16:23

标签: android view admob

我设法将adMob添加到标题屏幕,该布局用* .xml文件编写。

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

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

public class Game extends Activity{

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

如何将adMob添加到游戏中?

1 个答案:

答案 0 :(得分: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>