Textview vertical scroll:
When there are many welcome words in the project, you need to implement up and down scrolling. I learned that textview in android can easily achieve the horizontal running horse light effect, but there is no direct support for vertical scrolling, So Baidu Google, google did not find a ready-to-use demo after hundreds of times. So I did my research and wrote a demo that could implement vertical scrolling of textview. Due to project requirements, here I am using the absolutelayout layout. When the left and right keys are switched, the scrolling content is changed. I hope this demo can help kids with the same requirements!
<? XML version = "1.0" encoding = "UTF-8"?>
<Absolutelayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: Orientation = "vertical">
<Textview
Android: Id = "@ + ID/tscroll"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: maxlines = "5"
Android: scrollbars = "NONE"
Android: singleline = "false"
Android: textcolor = "# ff0000">
</Textview>
</Absolutelayout>
Package Sue. test;
Import java. util. arraylist;
Import java. util. List;
Import com. amttgroup. element. container;
Import com. amttgroup. element. rootlayout;
Import com. amttgroup. element. text;
Import com. amttgroup. utils. g;
Import Android. App. activity;
Import Android. Graphics. color;
Import Android. OS. Bundle;
Import Android. OS. Handler;
Import Android. util. log;
Import Android. View. gravity;
Import Android. View. keyevent;
Import Android. widget. absolutelayout;
Import Android. widget. textview;
Public class textscrollactivity extends activity {
Textview TV;
String L = "textscrollactivity ";
List <string> welcomewords = new arraylist <string> ();
Int curindex = 0;
@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Welcomewords
. Add ("it is a great honor for us to stay here. On behalf of all the staff of Beijing Hotel, I would like to extend my sincere welcome to you. The Beijing Hotel, founded in 1900, is a luxury hotel with a long history. It has luxurious and elegant rooms, unique dishes, conference and exhibition facilities, and entertainment and fitness facilities. We will wholeheartedly provide you with satisfactory and comfortable services. We hope you will have a pleasant stay at the Beijing Hotel. \ N your stay makes us feel honored. On behalf of all the staff of Beijing Hotel, I would like to extend my sincere welcome to you. The Beijing Hotel, founded in 1900, is a luxury hotel with a long history. It has luxurious and elegant rooms, unique dishes, conference and exhibition facilities, and entertainment and fitness facilities. We will wholeheartedly provide you with satisfactory and comfortable services. We hope you will have a pleasant stay at the Beijing Hotel. ");
Welcomewords
. Add ("it is an honor for you to stay at the Beijing Hotel. on behalf of the staff at the Beijing Hotel, I sincerely welcome you. built in 1900, Beijing Hotel is a luxury hotel with a long history. we have elegant guestrooms, exquisite cuisine,
Convenient Facilities and entertainment facilities. It is our pleasure to offer you the best services. Have a nice stay! ");
Setcontentview (R. layout. textscroll );
TV = (textview) findviewbyid (R. Id. tscroll );
Absolutelayout. layoutparams Lp = (absolutelayout. layoutparams) TV
. Getlayoutparams ();
LP. x = 300;
LP. Y = 300;
LP. width = 500;
LP. Height = 170;
TV. settextsize (16 );
TV. settextcolor (color. White );
TV. setgravity (gravity. Left );
TV. settext (welcomewords. Get (curindex ));
H. postdelayed (R, 3000 );
}
Handler H = new handler ();
Int I = 0;
Runnable r = new runnable (){
@ Override
Public void run (){
Int Height = TV. getheight ();
Int scrolly = TV. getscrolly ();
Int lineheight = TV. getlineheight ();
Int linecount = TV. getlinecount (); // total number of lines
Int Maxy = (TV. getlinecount () * TV. getlineheight ()
+ TV. getpaddingtop () + TV. getpaddingbottom ())
-TV. getheight ();
Log. E ("= Maxy =", Maxy + "");
Log. E ("= height =", height + "");
Log. E ("= linecount =", TV. getlinecount () + "");
Double viewcount = math. Floor (height/lineheight); // maximum number of rows displayed in the visible area
If (linecount> viewcount) {// if the total number of rows is greater than the number of rows displayed in the visible area, the page will scroll.
If (scrolly> = Maxy ){
TV. scrollby (0,-Maxy );
} Else {
TV. scrollby (0, lineheight );
}
H. postdelayed (this, 3000 );
}
}
};
Public Boolean onkeydown (INT keycode, keyevent event ){
Switch (keycode ){
Case keyevent. keycode_dpad_up:
Break;
Case keyevent. keycode_dpad_down:
Break;
Case keyevent. keycode_dpad_right:
Handle ();
Break;
Case keyevent. keycode_dpad_left:
Handle ();
Break;
Case keyevent. keycode_dpad_center:
Handle ();
Break;
Case keyevent. keycode_enter:
Handle ();
Break;
Case keyevent. keycode_back:
Finish ();
Break;
Default:
}
Return super. onkeydown (keycode, event );
}
Public void handle (){
H. removecallbacks (R );
Curindex = (curindex + 1) % 2;
TV. settext (welcomewords. Get (curindex ));
H. postdelayed (R, 3000 );
}
@ Override
Public void ondestroy (){
Super. ondestroy ();
H. removecallbacks (R );
}
}