2

我正在尝试在另一个视图之上添加一个视图。

首先,我通过 xml 设置视图,然后我想以编程方式添加第二个视图。public void onCreate(Bundle savedInstanceState)

{
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  LinearLayout ll = (LinearLayout) findViewById(R.id.layout);
  drawView = new DrawView(this);
  drawView.setBackgroundColor(Color.TRANSPARENT);
  Bitmap bitmap = Bitmap.createBitmap(10, 100, Bitmap.Config.ARGB_8888);
  Canvas canvas = new Canvas(bitmap);
  drawView.draw(canvas);
  drawView.setLayoutParams(new LayoutParams(800, 0, 0.18f));
  LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)drawView.getLayoutParams();
  params.setMargins(0, 0, 0, -130);
  drawView.setLayoutParams(params);
  ll.addView(drawView, 2);

}

我的问题是 xml 位于我要添加的视图之上。

我怎样才能使第二个视图在顶部?

4

3 回答 3

2

LinearLayout是一种布局,将其子级一个接一个地垂直或水平排列。如果您想将视图堆叠在另一个之上,请使用允许这样做的布局,例如 a RelativeLayout,而不是LinearLayout.

于 2012-04-19T07:58:24.473 回答
2

如果您想将视图堆叠在一起,您应该使用相对布局。因此,在您的情况下,将“布局”替换为相对布局,在 Java 代码中您可以执行以下操作。

    RelativeLayout.LayoutParams baseLayoutParam = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    baseLayout.setLayoutParams(baseLayoutParam);
    DrawView drawView = new DrawView(context);
    RelativeLayout.LayoutParams rLayoutParams = new RelativeLayout.LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, (int) (250 * Utility.getDip(context)));
    rLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT); // Adjust accordingly to your requirement
    drawView.setLayoutParams(rLayoutParams);ll.addView(drawView);
于 2012-04-19T08:02:27.007 回答
0

您可以调用视图的bringToFront方法:

drawView.bringToFront();
于 2012-04-19T07:58:19.853 回答