如何在下面放置adView?

时间:2011-05-10 15:31:52

标签: android layout android-linearlayout

我创建了一个游戏,其中包含从surfaceView派生的自定义视图。在游戏中我想将adView放在gameView下面。但问题是,如果我将adView放在gameView下面,adView就不会出现..我在代码中进行了所有布局,因为它更容易维护..

这是代码..

如果我将adView置于gameView上方,它将显示

private RelativeLayout layout;   //the gameView container
private LinearLayout mainLayout;  //the main layout
private RocketView rocketView;   //the gameView
private AdView adView;             //the adView
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

        mainLayout = new LinearLayout(this);
        mainLayout.setOrientation(LinearLayout.VERTICAL);

        adView = new AdView(this, AdSize.BANNER, "a14d9e9ea3740e8");
        adView.setVisibility(AdView.VISIBLE);
        adView.loadAd(new AdRequest());
        mainLayout.addView(adView);   // -->> here i place the adView first

        layout = new RelativeLayout(this);
        mainLayout.addView(layout);   //--->> then i place the gameView later

        rocketView = new RocketView(this);
        layout.addView(rocketView);
        setContentView(mainLayout);
}

但是,如果我将adView置于下方,则不会显示。

private RelativeLayout layout;
private LinearLayout mainLayout;
private RocketView rocketView;
private Launcher launcher;

private AdView adView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        mainLayout = new LinearLayout(this);
        mainLayout.setOrientation(LinearLayout.VERTICAL);

        layout = new RelativeLayout(this);
        mainLayout.addView(layout);

        rocketView = new RocketView(this);
        layout.addView(rocketView);

        adView = new AdView(this, AdSize.BANNER, "a14d9e9ea3740e8");
        adView.setVisibility(AdView.VISIBLE);
        adView.loadAd(new AdRequest());
        mainLayout.addView(adView);
        setContentView(mainLayout);

}

我想我知道发生了什么...... 在adView加载adRequest并获取广告后,它不会拉伸到上方,但它会拉伸到下方,因此它不会显示在屏幕上。但我不知道如何解决这个问题。请帮帮我..

1 个答案:

答案 0 :(得分:2)

首先,您不需要线性布局mainLayout。将rocketView和adView添加到您的relativelayout布局中。为adView提供align_parent_bottom,wrap_content(height)和wrap_content或fill_parent属性的宽度。然后给出rocketView fill_parent的宽度和高度,让它高于“adView”。