2012-06-28 7 views
10

ジェスチャーオーバレイビューの子供(レイアウト)を、そのビューにタッチした位置に追加するにはどうすればいいですか?位置の座標に合わせて配置することは可能ですか?GestureOverlayViewに子供を追加して、アンドロイドで触れたポイントを教えてください。

+0

あなたは私のソリューションを使用してみましたかあなたがあなた自身を発見した:私はそれがこのような何かを行くだろうと思いますか? – vArDo

答えて

1

API 11(Android 3.0.x)以上の場合:View.setX()View.setY()を使用できます。詳細は、API docsを参照してください。

すべてのAPIバージョン:RelativeLayoutRelativeLayout.Layoutを使用できます。具体的には、ViewGroup.MarginLayoutParamsから継承したフィールドにmarginsを設定します。

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); 
    } 
関連する問題