Kann man die absolute Position einer Ansicht in Android einstellen? (Ich weiß, dass es eine AbsoluteLayout
gibt, die aber veraltet ist ...)
Wenn ich beispielsweise einen Bildschirm mit 240x320px habe, wie könnte ich dann eine ImageView
hinzufügen, die 20x20px ist, so dass sich ihr Mittelpunkt in der Position (100, 100) befindet?
Sie können RelativeLayout verwenden. Angenommen, Sie wollten eine 30 x 40-Bildansicht an Position (50, 60) in Ihrem Layout. 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);
Mehr Beispiele:
Platziert zwei 30x40-ImageViews (eine gelbe und eine rote) bei (50,60) bzw. (80,90):
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);
Platziert eine 30x40 gelbe ImageView bei (50,60) und eine weitere 30x40 rote ImageView <80,90> relativ zu die gelbe ImageView:
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);
Im Allgemeinen können Sie Sie können eine Ansicht an einer bestimmten Position hinzufügen, indem Sie ein FrameLayout als Container verwenden, indem Sie die Attribute leftMargin und topMargin angeben .
Im folgenden Beispiel wird ein 20 x 20 Pixel großes ImageView an Position (100.200) platziert, wobei ein FrameLayout als Vollbild-Container verwendet wird:
XML
<FrameLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:tools="http://schemas.Android.com/tools"
Android:id="@+id/root"
Android:background="#33AAFF"
Android:layout_width="match_parent"
Android:layout_height="match_parent" >
</FrameLayout>
Aktivität/Fragment/Benutzerdefinierte Ansicht
//...
FrameLayout root = (FrameLayout)findViewById(R.id.root);
ImageView img = new ImageView(this);
img.setBackgroundColor(Color.RED);
//..load something inside the ImageView, we just set the background color
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(20, 20);
params.leftMargin = 100;
params.topMargin = 200;
root.addView(img, params);
//...
Dies ist der Trick, da Ränder als absolute Koordinaten (X, Y) ohne RelativeLayout verwendet werden können:
Um Andy Zhangs Antwort weiter oben hinzuzufügen, können Sie rl.addView mit param versehen und später Änderungen daran vornehmen.
params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = 50;
params.topMargin = 60;
rl.addView(iv, params);
Könnte genauso gut geschrieben werden als:
params = new RelativeLayout.LayoutParams(30, 40);
rl.addView(iv, params);
params.leftMargin = 50;
params.topMargin = 60;
Wenn Sie also die Variable params beibehalten, können Sie das Layout von iv jederzeit ändern, nachdem Sie es zu rl hinzugefügt haben.
Ein sauberer und dynamischer Weg, ohne Pixelwerte im Code fest zu codieren.
Ich wollte einen Dialog (den ich spontan aufblase) genau unter einem angeklickten Button positionieren.
und gelöst auf diese Weise:
// get the yoffset of the position where your View has to be placed
final int yoffset = < calculate the position of the view >
// position using top margin
if(myView.getLayoutParams() instanceof MarginLayoutParams) {
((MarginLayoutParams) myView.getLayoutParams()).topMargin = yOffset;
}
Sie müssen jedoch sicherstellen, dass das übergeordnete Layout vonmyView
eine Instanz vonRelativeLayout
ist.
vollständiger Code:
// identify the button
final Button clickedButton = <... code to find the button here ...>
// inflate the dialog - the following style preserves xml layout params
final View floatingDialog =
this.getLayoutInflater().inflate(R.layout.floating_dialog,
this.floatingDialogContainer, false);
this.floatingDialogContainer.addView(floatingDialog);
// get the buttons position
final int[] buttonPos = new int[2];
clickedButton.getLocationOnScreen(buttonPos);
final int yOffset = buttonPos[1] + clickedButton.getHeight();
// position using top margin
if(floatingDialog.getLayoutParams() instanceof MarginLayoutParams) {
((MarginLayoutParams) floatingDialog.getLayoutParams()).topMargin = yOffset;
}
Auf diese Weise können Sie immer noch erwarten, dass sich die Zielansicht an alle Layout-Parameter anpasst, die mit Layout-XML-Dateien festgelegt wurden, anstatt diese Pixel/dps im Java-Code fest zu codieren.
Platzieren Sie eine beliebige Ansicht auf Ihren WunschX&YPunkt
Layoutdatei
<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:tools="http://schemas.Android.com/tools"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
tools:context="com.example.test.MainActivity" >
<AbsoluteLayout
Android:id="@+id/absolute"
Android:layout_width="match_parent"
Android:layout_height="match_parent" >
<RelativeLayout
Android:id="@+id/rlParent"
Android:layout_width="match_parent"
Android:layout_height="match_parent" >
<ImageView
Android:id="@+id/img"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:background="@drawable/btn_blue_Matte" />
</RelativeLayout>
</AbsoluteLayout>
</RelativeLayout>
Java-Klasse
public class MainActivity extends Activity {
private RelativeLayout rlParent;
private int width = 100, height = 150, x = 20, y= 50;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AbsoluteLayout.LayoutParams param = new AbsoluteLayout.LayoutParams(width, height, x, y);
rlParent = (RelativeLayout)findViewById(R.id.rlParent);
rlParent.setLayoutParams(param);
}
}
Erledigt
Für den Fall, dass es jemandem helfen kann, können Sie auch diesen Animator ViewPropertyAnimator wie unten beschrieben ausprobieren
myView.animate().x(50f).y(100f);
myView.animate().translateX(pixelInScreen)
Hinweis: Dieses Pixel ist nicht relativ zur Ansicht. Dieses Pixel ist das Pixel Position auf dem Bildschirm.
kredite an bpr10 antworten