How does Android textview Implement Message scrolling?

Source: Internet
Author: User

Step 1: Layout file, mainly in red

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical" Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent" Android: weightsum = "1">

<Scrollview Android: layout_width = "fill_parent"
Android: scrollbars = "vertical"
Android: fadingedge = "vertical"
Android: Id = "@ + ID/scrollview"
Android: layout_margintop = "10px"
Android: layout_marginleft = "10px"
Android: layout_marginright = "10px"
Android: layout_marginbottom = "10px"
Android: layout_height = "20dp">


<Linearlayout Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent">


<Textview Android: text = "first line" Android: textcolor = "# 1a1a1a" Android: Background = "# c1cdcd" Android: textsize = "13dp" Android: id = "@ + ID/textview1" Android: layout_width = "fill_parent" Android: layout_height = "20dp"> </textview>


<Textview Android: text = "Row 2" Android: textcolor = "# 1a1a1a" Android: Background = "# c1cdcd" Android: textsize = "13dp" Android: id = "@ + ID/textview2" Android: layout_width = "fill_parent" Android: layout_height = "20dp"> </textview>


<Textview Android: text = "Row 3" Android: textcolor = "# 1a1a1a" Android: Background = "# c1cdcd" Android: textsize = "13dp" Android: id = "@ + ID/textview3" Android: layout_width = "fill_parent" Android: layout_height = "20dp"> </textview>


<Textview Android: text = "Row 4" Android: textcolor = "# 1a1a1a" Android: Background = "# c1cdcd" Android: textsize = "13dp" Android: id = "@ + ID/textview4" Android: layout_width = "fill_parent" Android: layout_height = "20dp"> </textview>


</Linearlayout>


</Scrollview>

</Linearlayout>

 

 

Part 2: java files

Package com. Exam;

Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. OS. Handler;
Import Android. OS. logoff;
Import Android. OS. message;
Import Android. widget. scrollview;

Public class exampleactivity extends activity {
/** Called when the activity is first created .*/

Private scrollview;

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );

Scrollview = (scrollview) findviewbyid (R. Id. scrollview );
Scrollview. setverticalscrollbarenabled (false );
Looperthread mclockthread = new looperthread ();
Mclockthread. Start ();

}

// Handler used for inter-thread Communication
Class testhandler extends handler {
Public testhandler (low.logoff ){
Super (logoff );
}

@ Override
Public void handlemessage (Message MSG ){

Bundle bundle = msg. getdata ();
Int loop = bundle. getint ("loop ");
System. Out. println ("loop:" + loop );
If (loop = 4)
Scrollview. scrollto (0, 0); // changes the position of the scroll bar.
Else
Scrollview. scrollto (loop * 29, loop * 29 + 30 + loop * 1); // change the position of the scroll bar
Super. handlemessage (MSG );
}
}

// Multi-thread control loop

Class looperthread extends thread {

Public void run (){
Super. Run ();
Try {
Int loop = 0;
While (true ){
Thread. Sleep (3000 );
Message MSG = new message ();
Bundle bundle = new bundle ();
Bundle. putint ("loop", loop );
MSG. setdata (bundle );
New testhandler (Looper. getmainlooper (). sendmessage (MSG );
Loop ++;
If (loop = 5 ){
Loop = 0;
}
}

} Catch (interruptedexception e ){
E. printstacktrace ();
}

}
}

}

Related Article

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.