2012-06-28 27 views

Trả lời

1

Đối với API 11 (Android 3.0.x) trở lên: bạn có thể sử dụng View.setX()View.setY(). Xem API docs để biết chi tiết.

Đối với tất cả các phiên bản API: bạn có thể sử dụng RelativeLayoutRelativeLayout.Layout. Cụ thể bằng cách đặt margins trong các trường được kế thừa từ ViewGroup.MarginLayoutParams. Tôi đoán nó sẽ đi một cái gì đó như thế này:

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); 
    } 
Các vấn đề liên quan