2012-06-28 12 views

risposta

1

Per API 11 (3.0.x Android) e soprattutto: è possibile utilizzare View.setX() e View.setY(). Vedi API docs per i dettagli.

Per tutte le versioni API: si potrebbe usare RelativeLayout e RelativeLayout.Layout. Specificamente impostando margins nei campi ereditati da ViewGroup.MarginLayoutParams. Credo che sarebbe più o meno così:

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); 
    } 
Problemi correlati