防止多行文本视图中的自动换行符



我知道这个问题已经被问过好几次了,但可能不完全是为了这个要求。

我想创建一个日志视图,所以目前基于TextView,显然,就像在所有日志视图中一样,例如在 AS 的 logcat 中,我将有几行,以便我可以启用vertical scrollbar并且可以有很长的日志行,每行都以一个n结尾。
在这里,我希望horizontal scrollbar能帮助我。但似乎不是,对吧?

如果没有,怎么办?

编辑我当前的文本视图定义:

<TextView
android:id="@+id/tvLog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:background="@android:color/darker_gray"
android:ems="10"
android:gravity="fill_horizontal"
android:inputType="none"
android:lines="5"
android:maxLines="20"
android:scrollbars="horizontal|vertical"
android:singleLine="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/elvTests" />

据我了解,您希望显示所有文本,并且可以向下滚动它,因此我想您可以执行类似操作

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/long_text"/>
</ScrollView>

将您的文本视图放在宽度和高度与其父视图匹配的滚动视图中,并将具有wrap_content的文本视图置于启用此行为希望这有所帮助

编辑

把它放在应该完成工作的代码中

TextView textview= (TextView) findViewById(R.id.your_textview_id);
textview.setMovementMethod(new ScrollingMovementMethod());

最新更新