Примерно так:
public class MyActivity extends Activity {
private RelativeLayout _mainLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
_mainLayout = (RelativeLayout) findViewById(R.id.canvas);
addChild(50, 50, 150, 150, Color.RED);
addChild(150, 250, 50, 50, Color.GREEN);
addChild(200, 200, 100, 100, Color.BLUE);
}
public void addChild(int centerX, int centerY, int width, int height, int color) {
int X = centerX - width / 2;
int Y = centerY - height / 2;
RelativeLayout child = new RelativeLayout(this);
child.setBackgroundColor(color);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(width, height);
layoutParams.leftMargin = X;
layoutParams.topMargin = Y;
layoutParams.bottomMargin = -250;
layoutParams.rightMargin = -250;
child.setLayoutParams(layoutParams);
_mainLayout.addView(child);
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:id="@+id/canvas"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg0large"
>
</RelativeLayout>