无法在 Scollable RelativeLayout 中滚动 TextView

Unable to scroll TextView inside a Scollable RelativeLayout

我正在尝试在滚动视图中创建一个带有 RelativeLayout 的 android 应用程序,该应用程序具有带滚动条的 TextView,但每次我尝试滚动 TextView 时,RelativeLayout 都会滚动。

我也试过以下解决方案,但没有任何效果:

  1. https://www.viralandroid.com/2015/10/how-to-make-scrollable-textview-in-android.html

  2. how to make a relative layout scrollable when it has many children views?

  3. RelativeLayout Scrollable

*我的布局看起来与第 3 个几乎相似 link 但控件更多。

以下是我的布局代码

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ScrollView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbars="vertical">
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:scrollbars = "vertical">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="365dp"
            android:layout_height="20dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="10dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="5dp"
            android:text="@string/text1" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="365dp"
            android:layout_height="40dp"
            android:layout_below="@+id/textView1"
            android:layout_alignParentStart="true"
            android:layout_marginStart="10dp"
            android:layout_marginEnd="5dp"
            android:inputType="text"
            android:text=""/>

        <!-- Other Controls, about 15 of them -->

        <TextView
            android:id="@+id/testLogs"
            android:layout_width="360dp"
            android:layout_height="150dp"
            android:layout_below="@+id/something"
            android:layout_alignParentStart="true"
            android:layout_marginStart="14dp"
            android:layout_marginTop="15dp"
            android:layout_marginEnd="14dp"
            android:background="@drawable/edit_text_background"
            android:textColor="@color/editTestBorderColor"
            android:enabled="true"
            android:gravity="bottom"
            android:hint="Process logs"
            android:scrollbars="vertical"
            android:textIsSelectable="true"
            android:focusable="true"
            android:longClickable="true"
            android:textSize="10dp" />

        <Button
            android:id="@+id/btnExecuteTest"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/editTextLogs"
            android:layout_alignParentStart="true"
            android:layout_marginStart="67dp"
            android:layout_marginTop="10dp"
            android:text="@string/btnExecute" />

    </RelativeLayout>
</ScrollView>

问题:无法滚动 Textview (android:id="@+id/testLogs")

如果我的控件较少(只要未激活布局的可滚动),TextView 的滚动就可以正常工作。

以下是设备规格。我用来测试的:

  1. 名称:华为 P10 lite
  2. OS : Android 8.0

我不知道为什么 TextView 滚动不工作? 请帮忙!

请不要介意编码结构...谢谢。

在您的布局中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ScrollView01"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:scrollbars="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        tools:context=".MainActivity">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="10dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="5dp"
            android:text="Enter SSID" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="365dp"
            android:layout_height="40dp"
            android:layout_below="@+id/textView1"
            android:layout_alignParentStart="true"
            android:layout_marginStart="10dp"
            android:layout_marginEnd="5dp"
            android:inputType="text"
            android:text="" />

        <!-- Other Controls, about 15 of them -->
        <androidx.core.widget.NestedScrollView
            android:id="@+id/scroll1"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_below="@+id/editText1"
            android:layout_marginTop="50dp"
            android:fillViewport="false"
            android:isScrollContainer="false">

            <TextView
                android:id="@+id/testLogs"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:layout_below="@+id/editText1"
                android:layout_alignParentStart="true"
                android:background="@color/colorPrimary"
                android:enabled="true"
                android:focusable="true"
                android:hint="Process logs"
                android:longClickable="true"
                android:scrollbars="vertical"
                android:text="@string/sample_text"
                android:textColor="@color/WindowBackground"
                android:textIsSelectable="true"
                android:textSize="14sp" />
        </androidx.core.widget.NestedScrollView>

        <Button
            android:id="@+id/btnExecuteTest"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/scroll1"
            android:layout_alignParentStart="true"
            android:layout_marginStart="134dp"
            android:layout_marginTop="10dp"
            android:text="Execute Test" />


        <!--This is Additional code bellow -->
        <!-- if you want to view multiple TextView with scrollable -->


        <androidx.core.widget.NestedScrollView
            android:id="@+id/scroll2"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_below="@+id/btnExecuteTest"
            android:layout_marginTop="50dp"
            android:fillViewport="false"
            android:isScrollContainer="false">


            <TextView
                android:id="@+id/textView99"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:layout_below="@+id/btnExecuteTest"
                android:text="@string/sample_text" />
        </androidx.core.widget.NestedScrollView>

        <androidx.core.widget.NestedScrollView
            android:id="@+id/scroll3"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_below="@+id/scroll2"
            android:layout_marginTop="50dp"
            android:fillViewport="false"
            android:isScrollContainer="false">

            <TextView
                android:id="@+id/textView100"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:layout_below="@+id/textView99"
                android:text="@string/sample_text" />
        </androidx.core.widget.NestedScrollView>

    </RelativeLayout>
</ScrollView>

在你activity:

OnCreate():

    TextView textView = findViewById( R.id.testLogs );
    TextView textView1 = findViewById( R.id.textView99 );
    TextView textView2 = findViewById( R.id.textView100 );
    textView.setMovementMethod( new ScrollingMovementMethod( ) );
    textView1.setMovementMethod( new ScrollingMovementMethod( ) );
    textView2.setMovementMethod( new ScrollingMovementMethod( ) );

预览:

图片1:

图片二:

试试这个

在XML文件中

android:maxLines = "AN_INTEGER"

android:scrollbars = "vertical"

并在 java

  tv_desc.setMovementMethod(new ScrollingMovementMethod());


  scrollview.setOnTouchListener(new OnTouchListener() { 

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        tv_desc.getParent().requestDisallowInterceptTouchEvent(false);

        return false;
    }
});

 tv_desc.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        tv_desc.getParent().requestDisallowInterceptTouchEvent(true);

        return false;
    }
});