开发者

How to add AdMob to ViewClass in Android

I manage to add adMob to the title screen, which layout is written in a *.xml-file.

setContentView(R.layout.main); 
AdView adView = (AdView)findViewById(R.id.ad);
a开发者_如何学JAVAdView.requestFreshAd();

main.xml contains:

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

The game itself is written in Tttview.

public class TttView extends View

The Activity Game.java create a instance of TttView and uses setcontent(tttView) to use it as layout.

public class Game extends Activity{

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

How can I add adMob to the game itself?


Well it's not obvious from your question what tttView is or how it is setup, but if it creates a layout type to display you can add a AdView programatically into a LinearLayout or similar like this:

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

Or if you are loading the layout from an XML file you can do similar what you have above

    setContentView(R.layout.game); 

Where game.xml is:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜