How to display large amounts of text in Android with TextView

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.