【问题标题】:Disable text wrapping in Android在 Android 中禁用文本换行
【发布时间】: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


【解决方案1】:

我终于找到了一个不错的解决方案!基本上,在我的自定义View 中,每当发生配置更改并启用换行时,我都会将宽度设置为屏幕宽度(我也在View 首次初始化时设置它):

@Override
protected void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    if (/* line wrapping enabled */)
        setWidth(Resources.getSystem().getDisplayMetrics().widthPixels);
    else setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
}

这只是因为,在我的例子中,EditText 填满了整个屏幕,所以将宽度设置为所需的值非常容易。在我尝试使用 MATCH_PARENTWRAP_CONTENT 之前,但在 HorizontalScrollView 中,它并没有像我希望的那样启用文本换行。

请注意,View 包含在 ScrollView 中的 HorizontalScrollView 中,因此默认情况下它会水平和垂直滚动。

希望这对遇到此问题的其他人有所帮助!

编辑:如果之前启用了文本换行并且您尝试使用此方法禁用它,则文本仍将被换行,因为根据定义,内容已被换行。我能想到的唯一解决方法是完全重新加载View 及其ScrollViews。如果您找到其他解决方案,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-14
    • 2012-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-15
    • 1970-01-01
    • 2010-11-24
    相关资源
    最近更新 更多