Recently learning Android, trying to achieve a simple display of the news demo, encountered a problem: a lot of news content, put on the textview above the screen, how to break?
Looked up the information, found two ways to achieve:
1. Use only TextView, with the TextView to complete the display of the scroll bar , in the layout XML file, TextView properties need to set Android:scrollbars and Android:singleline, as follows:
<TextView
Android:id= "@+id/news_item_content_text_view"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:linespacingextra= "2DP" android:scrollbars= "vertical" android:singleline= "false" Android:text= "This is content, Blablabla ..."Android:textcolor= "@color/news_content_color" />
Mainly yellow, and then add the code as follows in the activity's OnCreate or fragment onviewcreated method:
TextView Contenttv = (TextView) View.findviewbyid (R.id.news_item_content_text_view); Contenttv.setmovementmethod (Scrollingmovementmethod.getinstance ());
Basically OK here, but because in my demo, it shows two fragment in an activity, a news list on the left, a news detail on the right, and then a question: show a long story, then drag the news content to the end, after the news item is switched , display news content of TextView no content display, need to touch the TextView area to display, processing method:
After each news switch, add a TextView scroll bar after the TextView SetText method, as follows:
TextView Contenttv = (TextView) View.findviewbyid (R.id.news_item_content_text_view); Contenttv.settext (content); Contenttv.scrollto (0, 0);//scroll bar to 0 position
This will be OK.
2. The layout of the time to put TextView in a scrollview inside , this is more simple, do not need any code processing.
<ScrollView android:layout_width= "Match_parent" android:layout_height= "0DP" android:layout_weight= "1" > <TextView android:id= "@+id/news_item_content_text_ View " android:layout_width=" Match_parent " android:layout_height=" Wrap_content " Android:linespacingextra= "2DP" android:text= "This is content, Blablabla ..." Android:textcolor= "@color/news_content_color"/> </ScrollView>
When the TextView text content is very long, ScrollView automatically displays the scroll bar, does not need us to write the code to implement.
How to display large amounts of text in Android using TextView