简体   繁体   English

如何设置TextView以在Android中的一行中显示文本

[英]How to set a TextView to display text in one line in Android

I've set a TextView as the display. 我已将TextView设置为显示。 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. 问题是,当输入到达TextView的边缘时 - 它会跳转到下一行。

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. 您应该在TextView标记中使用android:maxLines="1" android:singleLine="true" is deprecated . android:singleLine="true"弃用

android:singleLine="true"

is the answer 是答案

Update : 更新:

android:singleLine attribute has been deprecated since API Level 3, you can use android:singleLine属性自API Level 3以来已被弃用,您可以使用

android:maxLines="1"

tl;dr : tl;博士

For new users seeing this question, use android:singleLine="true" . 对于看到此问题的新用户,请使用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. 根据此错误报告 ,设置android:maxLines="1"可能会导致您的应用程序在android:ellipsize中设置某些特定值时崩溃。 Apparently it affects most Android devices running 4.1 Jellybean through 6.0.1 Marshmallow (being fixed in 7.0 Nougat). 显然它会影响运行4.1 Jellybean到6.0.1 Marshmallow的大多数Android设备(固定在7.0牛轧糖中)。 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. 但是,如果您作为开发人员瞄准各种设备,最好使用android:singleLine="true"以防止崩溃。

在Textview中添加代码

android:singleLine="true"

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

android:ellipsize="none"

Set the TextView to singeline. 将TextView设置为singeline。

Take a look at this 看看这个

If you want to manually scroll your single line content then you should use TextView inside HorizontalScrollView 如果要手动滚动单行内容,则应在Horizo​​ntalScrollView中使用TextView

<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. 希望这会帮助你。

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

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