在MySurfaceView的顶部和底部添加其他内容

时间:2012-03-07 12:18:04

标签: android layout admob surfaceview

我正在设计一个包含MySurfaceView的Android应用程序,其中包含一个Canvas,用于在运行应用程序的线程中绘制图像,

(布局是默认的main.xml,用项目创建)

但我必须在顶部添加一个bar / textView并在屏幕的底部添加一个AD。 那怎么办呢?

我的声明是这样的:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mySurfaceView = new MySurfaceView(this);
    setContentView(mySurfaceView);              
}

可能是我提供的信息不足以了解情况,所以如果你发现我的问题有任何疑问,请问我

提前致谢

1 个答案:

答案 0 :(得分:2)

嗯,有一种可能性是你可以把它放在RelativeLayout或类似的东西里面吗?这假设你不想在xml中定义它可能看起来更干净。代码可能看起来像这样(你可以使它更有效/只使用LinearLayout,我只是把我头脑中的第一件事扔掉):

  RelativeLayout layout = new RelativeLayout(this);
  RelativeLayout.LayoutParams paramsTop =
          new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
                                          RelativeLayout.LayoutParams.WRAP_CONTENT);
  // Add the textView to the top
  paramsTop.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);

  TextView textView = new TextView(this);
  textView.setText("Hello World");
  // Give them id's since we need to position relative to one another
  textView.setId(1);
  layout.addView(textView, paramsTop);


  AdView adView = new AdView(this, AdSize.BANNER, INSERT_ADMOB_ID_HERE);
  adView.setId(2);
  RelativeLayout.LayoutParams paramsBottom =
      new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                                      RelativeLayout.LayoutParams.WRAP_CONTENT);
  // Add the adView to the bottom
  paramsBottom.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);

  layout.addView(adView, paramsBottom);
  AdRequest adRequest = new AdRequest();
  adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
  adView.loadAd(adRequest);


  SurfaceView view = new SurfaceView( this );
  view.setId(3);
  RelativeLayout.LayoutParams paramsMiddle =
          new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
                                          RelativeLayout.LayoutParams.WRAP_CONTENT);

  // Add the surfaceView in between the textView and the adView
  paramsMiddle.addRule(RelativeLayout.BELOW, textView.getId());
  paramsMiddle.addRule(RelativeLayout.ABOVE, adView.getId());
  layout.addView(view, paramsMiddle);

  setContentView(layout);