无法在Scollable RelativeLayout内部滚动TextView

时间:2019-02-21 07:18:33

标签: android textview scrollview

我正在尝试在具有带滚动条的TextView的滚动视图内使用RelativeLayout创建一个android应用,但是每次我尝试滚动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个链接相似,但具有更多控件。

以下是我的布局代码

<?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. 名称:Huawei P10 lite
  2. 操作系统:Android 8.0

我不知道为什么TextView滚动si无法正常工作? 请帮忙!

2 个答案:

答案 0 :(得分: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;
    }
});

答案 1 :(得分:0)

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

在您的布局中:

<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>

您的活动:

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:

enter image description here

图片2:

enter image description here