【发布时间】:2021-08-05 11:06:14
【问题描述】:
我正在开发一个 Android 项目,我有一个 TextView(确切地说是 EditText)和一个启用或禁用该视图的文本环绕的设置。我已经搜索了互联网(很多),但仍然没有找到任何令人满意的解决方案。
目前我使用NestedScrollView 进行垂直滚动,然后动态插入两个部分布局之一,第一个仅包含EditText,第二个包含EditText 包裹在HorizontalScrollView 中。问题是我目前每次从另一个活动返回时都必须重新启动活动,以确保我不会意外地将两个孩子添加到NestedScrollView(导致异常)。此外,解释这一点的代码似乎有点庞大和混乱。
第一个部分布局:
<com.example.application.EditorView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/editor_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:layout_gravity="start|top"
android:importantForAutofill="no"
android:inputType="textMultiLine|textCapSentences"
android:padding="@dimen/def_padding"
android:textIsSelectable="true"
android:textSize="18sp"
tools:ignore="LabelFor,ScrollViewSize" />
第二个:
<android.widget.HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/hscroll"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.application.EditorView
android:id="@+id/editor_content"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="start|top"
android:background="@android:color/transparent"
android:importantForAutofill="no"
android:inputType="textMultiLine|textCapSentences"
android:padding="@dimen/def_padding"
android:textIsSelectable="true"
android:textSize="18sp"
tools:ignore="LabelFor,ScrollViewSize" />
</android.widget.HorizontalScrollView>
(EditorView 只是扩展了EditText,不会影响布局)
我还发现EditText 有一个setHorizontallyScrolling(true) 方法,但是当用户滑动时使用它我不会获得滑行效果,这在我看来不太友好。
说了这么多,我的问题是:在 Android 中,有没有办法在 HorizontalScrollView 中动态地启用 文本换行,或者可以选择将其切换为原生包含在 (自定义)EditText?
【问题讨论】:
-
您如何控制
EditText小部件的宽度?这就是决定文本如何换行的原因。理想情况下,您的问题将包含一个 minimal reproducible example,显示您当前正在做什么。
标签: java android textview android-edittext word-wrap