简体   繁体   English

在 Android 中禁用文本换行

[英]Disable text wrapping in Android

I'm working on a project in Android where I have a TextView (an EditText to be exact) and a setting to enable or disable text wrapping for that view.我正在 Android 中的一个项目上工作,我有一个TextView (确切地说是一个EditText )和一个为该视图启用或禁用文本换行的设置。 I have searched the internet (a lot ) but still haven't found any satisfying solution.我已经搜索了互联网(很多),但仍然没有找到任何令人满意的解决方案。

Currently I'm using a NestedScrollView for the vertical scrolling and then dynamically insert one of two partial layouts, the first containing just an EditText , the second with the EditText wrapped inside a HorizontalScrollView .目前,我正在使用NestedScrollView进行垂直滚动,然后动态插入两个部分布局中的一个,第一个仅包含EditText ,第二个将EditText包含在 Horizo HorizontalScrollView中。 The problem is that I currently have to restart the activity every time I return from another one to ensure I don't accidentally add two children to the NestedScrollView (causing an exception).问题是我目前每次从另一个活动返回时都必须重新启动活动,以确保我不会不小心将两个孩子添加到NestedScrollView (导致异常)。 Also, the code that would account for that seems a bit bulky and confusing.此外,解释这一点的代码似乎有点庞大和混乱。

The first partial layout:第一部分布局:

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

The second one:第二个:

<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 simply extends EditText and in no way effects the layout) EditorView只是扩展了EditText并且不会影响布局)

I've also found that EditText has a setHorizontallyScrolling(true) method, but using that I don't get a gliding effect when the user swipes which in my opinion feels less user friendly.我还发现EditText有一个setHorizontallyScrolling(true)方法,但是使用它时,当用户滑动时,我不会获得滑行效果,我认为这对用户不太友好。

Having said all that, my question is: In Android, is there a way to dynamically enable text wrapping inside a HorizontalScrollView , or to have an option to toggle it natively included inside a (custom) EditText ?说了这么多,我的问题是:在 Android 中,有没有办法在 Horizo HorizontalScrollView中动态启用文本换行,或者可以选择将其本地切换到(自定义) EditText中?

I've finally found a decent solution, Basically, in my custom View I set the width to the screen width whenever a configuration change occurs and line wrapping is enabled (I also set it when the View is first initialized):我终于找到了一个不错的解决方案,基本上,在我的自定义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);
}

This only works because, in my case, the EditText fills the entire screen, so setting the width to the desired value is quite easy.这只是因为在我的情况下, EditText填满了整个屏幕,因此将宽度设置为所需的值非常容易。 Before I tried using MATCH_PARENT and WRAP_CONTENT , but inside a HorizontalScrollView that doesn't enable text wrapping as I'd hoped.在我尝试使用MATCH_PARENTWRAP_CONTENT ,但在 Horizo HorizontalScrollView中,它没有像我希望的那样启用文本换行。

Note that this View is contained inside a HorizontalScrollView inside a ScrollView , so by default it would scroll both horizontally and vertically.请注意,此View包含在ScrollView内的HorizontalScrollView ntalScrollView 中,因此默认情况下它会水平和垂直滚动。

Hope this helps anyone else who comes across this problem!希望这可以帮助遇到此问题的其他人!

EDIT: If text wrapping was previously enabled and you try disabling it with this approach, the text will still be wrapped, as the content is, by definition, already wrapped.编辑:如果之前启用了文本换行并且您尝试使用这种方法禁用它,则文本仍将被换行,因为根据定义,内容已经被换行。 The only fix I can think of is completely reloading the View together with its ScrollViews .我能想到的唯一解决方法是完全重新加载View及其ScrollViews If you find another solution, please let me know.如果您找到其他解决方案,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM