2012-06-28 4 views

Antwort

1

Für API 11 (Android 3.0.x) und höher: können Sie View.setX() und View.setY() verwenden. Details finden Sie unter API docs.

Für alle API-Versionen: können Sie RelativeLayout und RelativeLayout.Layout verwenden. Insbesondere durch Setzen von margins in von ViewGroup.MarginLayoutParams geerbten Feldern. Ich denke, es so etwas wie dies gehen würde:

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