Disable Word Wrap in an Android Multi-Line TextVie

2020-02-24 12:34发布

问题:

I am working on a Month View with an advanced swipe (requires current, next and previous to be loaded to allow each view to stick to your finger) which means I have many views which causes things to be a little bit slow.

            |          | #<-- screen bounderies
 ' previous ' current  '   next   ' #<-- three months loaded
    ' previous ' current  '   next   ' #<-- three months when the user drags their finger

Because of this I want to represent multiple events in a single TextView. When the user taps one of the days on the Month View (small screen), it will open the day view for that day.

On each day in the month view (typically a single day gets one seventh of the width of the screen and a little less than one seventh vertically) I would like to avoid this

|    31|
|10a:  |
| Testi|
|ng    |
|11a:  |

Instead I want

|    31|
|10a:  |
| Testi|
|11a:  |
| Anoth|

Notice that the ng is cut off instead of wrapping to the next line. This is what I am looking for.

回答1:

In Java:

setHorizontallyScrolling(boolean) 

In theory, android:scrollHorizontally should do the same in XML, but there is a bug in android that stops it working.



回答2:

android:singleLine-----this is for xml

setTransformationMethod(TransformationMethod)-----this is for java

Constrains the text to a single horizontally scrolling line instead of letting it wrap onto multiple lines, and advances focus instead of inserting a newline when you press the enter key.

I'm not sure if this will work, but it worked for me when I wanted my TextView not to wrap in my ListView



回答3:

Currently the best answer for XML is

android:maxLines="1"

Because android:singleLine is now depreciated



回答4:

in java code

textView1.setMovementMethod(new ScrollingMovementMethod());

in XML file

android:minLines="5"

does the trick.