如何在我在该视图中触摸的位置在手势覆盖视图中添加子项(布局).....是否可以根据位置的坐标定位它?
问问题
438 次
2 回答
1
对于 API 11 (Android 3.0.x) 及更高版本:您可以使用View.setX()
和View.setY()
。有关详细信息,请参阅API 文档。
对于所有 API 版本:您可以使用RelativeLayout
和RelativeLayout.Layout
。特别是通过在继承自的字段中设置边距ViewGroup.MarginLayoutParams
。我想它会是这样的:
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);
于 2012-07-06T18:40:24.337 回答
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);
}
于 2012-08-20T18:19:14.673 回答