Android scrollable textview不能在scrollview中工作



我正在开发1个应用程序,在这个scrollview中我需要1个scrollview,我需要把滚动textview/webview(必须显示一些静态字符串),但必须在scrollview中滚动,但不工作

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@color/white" >  
    <ScrollView
            android:id="@+id/scrollView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/img_view_flag"
            android:layout_marginTop="17dp" 
            android:paddingBottom="20dip">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >

                    <TextView
                        android:id="@+id/tv_desc"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_below="@+id/rel_layout"
                        android:layout_marginTop="15dp"
                        android:maxLines="2"
                        android:scrollbars="vertical"
                        android:text="Medium Text"
                        android:paddingBottom="10dip"
                         android:textAppearance="?android:attr/textAppearanceSmall"
                        android:textColor="@color/black" />
     </LinearLayout>
        </ScrollView>
    </RelativeLayout>

在我的java文件中我做了…

scrollView1 = (ScrollView) findViewById(R.id.scrollView1);
    // scrollView2 = (ScrollView) findViewById(R.id.scrollView2);
    tv = (TextView) findViewById(R.id.tv_desc);
    tv.setText("this is 1n"+"this is 2"+"n this ix 3n"+"this is 4nn"+"this is 5n" );
    scrollView1.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            // TODO Auto-generated method stub Log.v("PARENT",
            // "PARENT TOUCH");
            Log.v("PARENT", "PARENT TOUCH");
            findViewById(R.id.tv_desc).getParent()
                    .requestDisallowInterceptTouchEvent(false);
            return false;
        }
    });
    tv.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            Log.v("TextView", "CHILD TOUCH");
            arg0.getParent().requestDisallowInterceptTouchEvent(true);
            return false;
        }
    });

但不为我工作....什么错了,或者我错过了什么?

你必须在你的代码中使用下面一行使textview可滚动。

yourTextView.setMovementMethod(new ScrollingMovementMethod())

找到解决办法了

在OnCreate()函数的末尾添加以下行

    scrollView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            textView.getParent().requestDisallowInterceptTouchEvent(false);
            return false;
        }
    });
    textView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            textView.getParent().requestDisallowInterceptTouchEvent(true);
            return false;
        }
    });

最新更新