簡體   English   中英

當RelativeLayout進入ScrollView時,觸摸移動TextView不能正常工作

[英]When RelativeLayout is into a ScrollView, moving TextView with touch doesn't work correctly

在我的應用中,我以編程方式創建了一個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代替RelativeLayout AbsoluteLayout將為TextView提供更多自由移動

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM