简体   繁体   中英

How to set a TextView to display text in one line in Android

I've set a TextView as the display. I want to display the operation being performed in one line, for example:

9856243187 + 2457896123 - 214583

The problem is, when the input reaches the edge of the TextView - it jumps to the next line.

So, is there anyway to avoid this behavior and keep the input in one line, even if it goes out of the screen?

You should use android:maxLines="1" in your TextView tag. android:singleLine="true" is deprecated .

android:singleLine="true"

is the answer

Update :

android:singleLine attribute has been deprecated since API Level 3, you can use

android:maxLines="1"

tl;dr :

For new users seeing this question, use android:singleLine="true" . Ignore deprecation warnings.

Explanation

According to this bug report , setting android:maxLines="1" may cause your application to crash when some specific values are set in android:ellipsize as well. Apparently it affects most Android devices running 4.1 Jellybean through 6.0.1 Marshmallow (being fixed in 7.0 Nougat). Depending on your device, your mileage may vary. However, if you as a developer are targeting a wide range of devices, it is best to use android:singleLine="true" in order to prevent crashes.

在Textview中添加代码

android:singleLine="true"

谢谢家伙单行做了技巧,但我也不得不删除省略号:

android:ellipsize="none"

Set the TextView to singeline.

Take a look at this

If you want to manually scroll your single line content then you should use TextView inside HorizontalScrollView

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="16dp"
        android:paddingTop="16dp"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:text="Single line text view that scrolls automatically if the text is too long to fit in the widget" />
</HorizontalScrollView>

这段代码对我有用:

android:singleLine="true"

Can you please try this one ?

<TextView
        android:text="Single line text view that scrolls automatically if the text is too long to fit in the widget" 
        android:singleLine="true" 
        android:ellipsize="marquee"
        android:marqueeRepeatLimit ="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true" 
        android:scrollHorizontally="true"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"/>

Hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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