Ответ 1
Для API 11 (Android 3.0.x) и выше: вы можете использовать View.setX()
и View.setY()
. Подробнее см. API docs.
Для всех версий API: вы можете использовать RelativeLayout
и RelativeLayout.Layout
. В частности, установив поля в полях, унаследованных от ViewGroup.MarginLayoutParams
. Я предполагаю, что это будет выглядеть примерно так:
RelativeLayout parentLayout = (RelativeLayout) findViewById(R.id.relative_layout);
LinearLayout newLayout = new LinearLayout(this); // or some other layout
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
// You can enter layout absolute dimensions here instead of 'wrap_content'.
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
// Set your X and Y position here for new layout.
layoutParams.setMargins(100 /*left*/, 200 /*top*/, 0 /*right*/, 0 /*bottom*/);
parentLayout.addView(newLayout , layoutParams);