如何在android中为文本视图设置每行最大字符数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10933412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:31:34  来源:igfitidea点击:

How to set maximum characters per line for text view in android

android

提问by dharan

Hiii all, this may be a possible duplicate but I couldn't find solution for my problem.I need to limit the number of characters per line when displaying on the textview and display the remaining characters in the next line.I cannot set ems value and I also tried setting maxLength attribute in xml but it is not displaying the remaining characters,Can any one tell me how can I do it.

大家好,这可能是重复的,但我找不到解决我的问题的方法。在 textview 上显示时,我需要限制每行的字符数,并在下一行显示剩余的字符。我无法设置 ems 值我也尝试在 xml 中设置 maxLength 属性,但它没有显示剩余的字符,谁能告诉我我该怎么做。

回答by prolink007

Using

使用

android:maxWidth="size"

Replace the "size" with the size you prefer. For example, 100dip.

用您喜欢的尺寸替换“尺寸”。例如,100dip

回答by Aerrow

Use this android:maxLength="10"

用这个 android:maxLength="10"

For more Information refer this site

有关更多信息,请参阅此站点

回答by Thkru

use android:maxWidth="10dip"to limit the size of the textview. With this limitation the next line (assume you used setSingleLine(false)) will start right after the 10dip.

用于android:maxWidth="10dip"限制文本视图的大小。有了这个限制,下一行(假设你使用了setSingleLine(false))将在10dip.

Using android:maxLength="50"for example would limit your textview, but doesn't ensure that it breaks after X characters

android:maxLength="50"例如使用会限制你的 textview,但不能确保它在 X 个字符后中断

回答by Jacek

Set these parameters of TextView:

设置 TextView 的这些参数:

android:layout_width="110dip"
android:singleline="false"

Experiment with the value of android:layout_widthto find the value that fits your needs.

试验 的值android:layout_width以找到适合您需要的值。

回答by Dan Alboteanu

use ConstraintLayout and android:layout_width="0dp". The text will adapt to the constraints

使用 ConstraintLayout 和 android:layout_width="0dp"。文本将适应限制

回答by Pouria Hemati

Use android:maxLength=""to limit the text length

使用android:maxLength=""限制文本长度

<TextView
        android:id="@+id/textViewName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:text="Name"
        android:maxLength="12"
        android:textColor="#000000"
        android:textStyle="bold" />

You can also use another property as follows:

您还可以使用另一个属性,如下所示:

android:ellipsize="end"
android:maxLines="1"

show result :

显示结果:

"Hello How are ..."instead of "Hello How are you?"

“你好,你好吗……”而不是“你好,你好吗?”

回答by Ganesh Katikar

Use this code:

使用此代码:

android:maxLines="5"
android:maxLength="10"

Five line with max length of 10 character.

五行,最大长度为 10 个字符。