Sie können RelativeLayout verwenden. Nehmen wir an, Sie wollten ein 30x40 ImageView an der Position (50,60) in Ihrem Layout haben. Irgendwo in Ihrer Aktivität:
// Some existing RelativeLayout from your layout xml
RelativeLayout rl = (RelativeLayout) findViewById(R.id.my_relative_layout);
ImageView iv = new ImageView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = 50;
params.topMargin = 60;
rl.addView(iv, params);
Weitere Beispiele:
Orte zwei 30x40 ImageViews (eine gelbe, eine rote) bei (50,60) und (80,90) verbunden sind:
RelativeLayout rl = (RelativeLayout) findViewById(R.id.my_relative_layout);
ImageView iv;
RelativeLayout.LayoutParams params;
iv = new ImageView(this);
iv.setBackgroundColor(Color.YELLOW);
params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = 50;
params.topMargin = 60;
rl.addView(iv, params);
iv = new ImageView(this);
iv.setBackgroundColor(Color.RED);
params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = 80;
params.topMargin = 90;
rl.addView(iv, params);
Orte eine 30x40 gelb Image bei (50,60) und einem weiteren 30x40 roten Image < 80,90>relativ zu das gelben Image:
RelativeLayout rl = (RelativeLayout) findViewById(R.id.my_relative_layout);
ImageView iv;
RelativeLayout.LayoutParams params;
int yellow_iv_id = 123; // Some arbitrary ID value.
iv = new ImageView(this);
iv.setId(yellow_iv_id);
iv.setBackgroundColor(Color.YELLOW);
params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = 50;
params.topMargin = 60;
rl.addView(iv, params);
iv = new ImageView(this);
iv.setBackgroundColor(Color.RED);
params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = 80;
params.topMargin = 90;
// This line defines how params.leftMargin and params.topMargin are interpreted.
// In this case, "<80,90>" means <80,90> to the right of the yellow ImageView.
params.addRule(RelativeLayout.RIGHT_OF, yellow_iv_id);
rl.addView(iv, params);
siehe auch 'view.setTranslationX()' oder 'view.offsetLeftAndRight()' –
Ich habe eine Bibliothek nur veröffentlicht, die hier von Interesse gewesen sein kann. github.com/ManuelPeinado/ImageLayout – Manuel
Dies ist so schwierig, weil 99,9% der Zeit absolute Positionierung ist eine schlechte Idee auf Android. Wenn Sie eine App schreiben, die * NUR * jemals auf einem physischen Gerät ausgeführt wird, könnte dies funktionieren, aber das ist im Allgemeinen keine sichere Annahme. Laden Sie das beispielsweise nicht in Google Play hoch.Es funktioniert gut auf iOS, da es nur eine Handvoll Hardwaregeräte gibt und Sie können für jedes ein eigenes Storyboard erstellen. – edthethird