Jak mogę dodać dziecko (układ) w widoku gestowym w punkcie, w którym dotknąłem tego widoku ... Czy możliwe jest ustawienie go zgodnie z współrzędnymi położenia?Dodawanie dziecka w GestureOverlayView w punkcie, w którym dotknąłem Androida?
10
A
Odpowiedz
1
Dla API 11 (Android 3.0.x) i nowszych wersji: można użyć View.setX()
i View.setY()
. Aby uzyskać szczegółowe informacje, patrz API docs.
Dla wszystkich wersji interfejsu API: można użyć RelativeLayout
i RelativeLayout.Layout
. W szczególności przez ustawienie margins w polach odziedziczonych po ViewGroup.MarginLayoutParams
. Chyba byłoby to mniej więcej tak:
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);
0
public boolean onTouchEvent(MotionEvent event) {
TextView tv=new TextView(this);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
x = event.getX();
y = event.getY();
RelativeLayout gh = (RelativeLayout) findViewById(R.id.relative1);
RelativeLayout.LayoutParams para = new RelativeLayout.LayoutParams(150, 50); //Size of textView
para.leftMargin = (int) x; //location of textview
para.topMargin = (int) y;
tv.setLayoutParams(para);
tv.setText("You have Touched at "+x+" ,"+y);
change();
gh.addView(tv);
}
Czy próbowałeś za pomocą mojego rozwiązanie lub znalazłeś własną rękę? – vArDo