Limit numbers of lines in TextView

跟風遠走 提交于 2021-02-05 17:52:47

问题


I have a scrollable textView, and I want to limit the number of lines displayed, however xml properties are not working :

<TextView
   android:id="@+id/tv_addesc"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:scrollbars="vertical"
   android:maxLines="12"
   android:textColor="#FFFFFFFF"
   android:textSize="15sp" />

the number of lines displayed is 50 and there are 900 chars in the text.

How can I limit the number of lines displayed and make it scrollable ?

Edit : I tested with 846 lines and 15824 chars, the whole text is displayed regardless of different properties set.

Edit : there was a second component besides the textView, when i removed it it worked, so I will find a workaround. Thank you !


回答1:


You just have to set a number of lines in your TextView like this:

android:maxLines = "10"

and you must also add:

android:minLines="1"

The rest of this not necessary if you are not using scrolling

and a property which says that this TextView should be scrollable vertically:

android:scrollbars = "vertical"

And in your Java-code:

yourTextView.setMovementMethod(new ScrollingMovementMethod())



回答2:


Find your textview by id:

TextView myTextBox=(TextView)findViewById(R.id.textBox);

Now use the setMaxLines function and assign the number of lines you require in it,say 20.

myTextBox.setMaxLines(20);

This limits your text box to show 20 lines only.




回答3:


put your text view with in scroll view and set fixed height of scroll view.

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="120dip" >

    <TextView
        android:id="@+id/tv_addesc"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:maxLines="12"
        android:scrollbars="vertical"
        android:textColor="#FFFFFFFF"
        android:textSize="15sp" />
</ScrollView>

set properties as per your need




回答4:


<ScrollView
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="120dip" >

<TextView
    android:id="@+id/tv_addesc"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:maxLines="12"
    android:scrollbars="vertical"
    android:textColor="#FFFFFFFF"
    android:textSize="15sp" />
 </ScrollView>



回答5:


hey try setting the singleline property to false. Just see if it works.




回答6:


I tried as said in accepted answer and others but didn't worked for me, but after adding android:ellipsize="marquee" it's working now..




回答7:


Try using this.

android:maxLines="12"
android:ellipsize="end"

In the end it add ... if extra text available.

You can also use

android:ellipsize="marquee" 

It will reduce extra text from end that is not like a word.



来源:https://stackoverflow.com/questions/12911765/limit-numbers-of-lines-in-textview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!