java - 当RelativeLayout进入ScrollView时,通过触摸移动TextView无法正常工作

标签 java android touch

在我的应用程序中,我以编程方式创建一个 TextView,用户可以通过触摸移动它。这是我的 MainActivity 代码:

    rleEditImage= (FrameLayout) findViewById(R.id.rleEditImage);

    TextView newTextView = new TextView(G.context);
    newTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    newTextView.setText("TextView");
    newTextView.setId(2);
    newTextView.setTextSize(26);
    rleEditImage.addView(newTextView);
    final TextView txt = (TextView) findViewById(2);
    txt.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View view, MotionEvent event) {
            final int X = (int) event.getRawX();
            final int Y = (int) event.getRawY();
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                _xDelta = X - lParams.leftMargin;
                _yDelta = Y - lParams.topMargin;
                break;
            case MotionEvent.ACTION_UP:
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                break;
            case MotionEvent.ACTION_POINTER_UP:
                break;
            case MotionEvent.ACTION_MOVE:
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                layoutParams.leftMargin = X - _xDelta;
                layoutParams.topMargin = Y - _yDelta;
                layoutParams.rightMargin = -250;
                layoutParams.bottomMargin = -250;
                view.setLayoutParams(layoutParams);
                break;
            }
            rleEditImage.invalidate();
            return true;
        }
    });

这是我的 .xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#dfdfdf"
android:orientation="vertical" >
<ScrollView
    android:id="@+id/srlCreate"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true"
     >
    <RelativeLayout
        android:id="@+id/rleEditImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        android:focusable="true"
        android:focusableInTouchMode="true" >
    </RelativeLayout>
</ScrollView>

但它不能正常工作。当我删除 ScrollView 时,它会正常工作。我该如何解决这个问题?

最佳答案

使用AbsoluteLayout而不是RelativeLayoutAbsoluteLayout将为TextView提供更自由的移动

关于java - 当RelativeLayout进入ScrollView时,通过触摸移动TextView无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28391361/

相关文章:

java - 使用线程在 JavaFX 中移动矩形

android - 是否有可能改进 BLE 服务发现

android - 如何从 OnClickListener 启动 Intent

java - 如何引用接口(interface)在 Java 中实现的类类型?

java - Java中静态方法不支持动态多态的原因

javascript - HTML5/Javascript - 是否可以在浏览器中获取 Macbook 触控板上触摸事件的绝对坐标?

Android:通过手指触摸屏幕来测量/检测覆盖区域(不仅是触摸坐标)

ios - 我的 iOS 应用程序经常崩溃 [UIGestureRecognizer _delegateShouldReceiveTouch :] of pan gesture

java - Android PDF生成

java - 在 Eclipse 中,当我转到 File->New-Project 时没有 Android Project