以编程方式在android游戏中包含布局(即非xml)

时间:2013-04-22 06:25:10

标签: android revmob

如何在Android中以编程方式包含布局而不使用xml布局在android中显示横幅广告。我正在创建一个游戏应用程序,我需要在每个游戏页面中显示横幅广告。我已经包含全屏revmob广告,但我需要包含横幅广告。我使用了以下代码,但横幅广告未显示

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

    <LinearLayout android:id="@+id/banner"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content" >
    </LinearLayout>

    </LinearLayout>

MainActivity.java

public class MainActivity extends Activity {
private CCGLSurfaceView mGLSurfaceView;
private boolean isCreated = false;
public static FrameLayout m_rootLayout;

public static String APPLICATION_ID = "514b9c57cce0500d00000001";
public static RevMob revmob;

// This is used to display Toast messages and is not necessary for your app
@Override
protected void onCreate(Bundle savedInstanceState) {
    if (!isCreated) {
        isCreated = true;
    } else {
        return;
    }

    super.onCreate(savedInstanceState);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_main);
     //admob widget

    revmob = RevMob.start(this, APPLICATION_ID);
     displayRevMob();

    mGLSurfaceView = new CCGLSurfaceView(this);
    setContentView(mGLSurfaceView);


    CCDirector.sharedDirector().attachInView(mGLSurfaceView);

    getScaledCoordinate();

    Global.assetManager = getAssets();
    Global.context = this;
    Global.loadUserInfo();
    CCScene scene = CCScene.node();
    scene.addChild(new SplashScene(), -1);

    CCDirector.sharedDirector().runWithScene(scene);




    //-------------IAP-----------------------
    Log.d(TAG1, "Creating IAB helper.");
        mHelper = new IabHelper(this, base64EncodedPublicKey);
        mHelper.enableDebugLogging(true);
        Log.d(TAG1, "Starting setup.");
        mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
            public void onIabSetupFinished(IabResult result) {
                Log.d(TAG, "Setup finished.");

                if (!result.isSuccess()) {
                    // Oh noes, there was a problem.
                    complain("Problem setting up in-app billing: " + result);
                    return;
                }

                // Hooray, IAB is fully set up. Now, let's get an inventory of stuff we own.
                Log.d(TAG, "Setup successful. Querying inventory.");
                mHelper.queryInventoryAsync(mGotInventoryListener);
            }
        });


      RevMobBanner banner = revmob.createBanner(this);
          ViewGroup view = (ViewGroup) findViewById(R.id.banner);
          view.addView(banner);

        Global.myActivity=this;
}

3 个答案:

答案 0 :(得分:2)

首先应该通过LayoutInflater从xml布局文件创建一个新视图。然后添加横幅。这是一个样本:

LayoutInflater inflater = LayoutInflater.from(getContext()); 
ViewGroup yourview = (ViewGroup)inflater.inflate(R.layout.yourlayout, null);

RevMobBanner banner = revmob.createBanner(this);
yourview.addView(banner);

如果你想在surfaceview上方使用linearlayout,一个简单的方法就是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

    <LinearLayout android:id="@+id/banner"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content" 
           android:orientation="vertical">
    </LinearLayout>

    <CCGLSurfaceView android:id="@+id/yoursurfaceview"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content">
    </CCGLSurfaceView>

</LinearLayout>

答案 1 :(得分:1)

您的XML的程序化翻译是;

LinearLayout lin=new LinearLayout(this);
lin.setLayoutsParams(new LayoutParams(LayoutParams.FILL_PARENT,
                                      LayoutParams.FILL_PARENT));
lin.setOrientation(LinearLayout.VERTICAL);

LinearLayout lin2=new LinearLayout(this);
lin2.setLayoutsParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                                      LayoutParams.WRAP_CONTENT));

lin.addView(lin2);

答案 2 :(得分:0)

您可以使用 LayoutInflater 例如

LayoutInflater inflater = getLayoutInflater();
View layOut = inflater.inflate(R.layout.some_layout_name, false, false);
//to fetch Views use 
TextView label_name = (TextView) layOut.findViewById(R.id.view_id_here);