我正在尝试在 ScrollView 中创建一个带有RelativeLayout的Android应用程序,该 View 有一个带滚动条的TextView,但每次我尝试滚动TextView时,RelativeLayout都会滚动。
我也尝试过以下解决方案,但没有任何效果:
https://www.viralandroid.com/2015/10/how-to-make-scrollable-textview-in-android.html
how to make a relative layout scrollable when it has many children views?
*我的布局看起来几乎与第三个链接相似,但有更多控件。
以下是我的布局代码
<?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 的滚动就可以工作。
以下是设备规范。我用于测试的:
- 名称:华为 P10 lite
- 操作系统:Android 8.0
我不知道为什么 TextView 滚动不起作用? 请帮忙!
最佳答案
试试这个
在 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;
}
});
关于android - 无法在可滚动相对布局内滚动 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54801296/