【问题标题】:Adding a child in a GestureOverlayView on the point where i touched in android?在我在 android 中触摸的位置在 GestureOverlayView 中添加一个孩子?
【发布时间】:2012-06-28 06:31:54
【问题描述】:

如何在我在该视图中触摸的位置在手势覆盖视图中添加一个子项(布局).....是否可以根据位置的坐标定位它??

【问题讨论】:

  • 您尝试过使用我的解决方案还是找到了自己的解决方案?

标签: android view gesture


【解决方案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);

【讨论】:

    【解决方案2】:
    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);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-11
      • 1970-01-01
      • 2018-01-25
      • 2022-01-25
      • 2011-03-29
      • 2016-06-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多