Functions and usage of TextSwitcher,
TextSwitcher integrates ViewSwitcher, so it has the same feature as ViewSwitcher: You can use animation effects when switching View components. Similar to ImageSwitcher, you also need to set a ViewFactory to use TextSwitcher. Unlike ImageSwitcher, The makeView () method of the ViewFactory required by TextSwitcher must return a TextView component.
<TextSwitcher and TextView have similar functions. They can be used to display text content. The difference is that TextSwitcher is more effective and can specify the animation effect during text switching.>
Let's not talk about the code. The interface layout file is as follows:
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent"> <! -- Defines a TextSwitcher and specifies the animation effect for text switching --> <TextSwitcher android: id = "@ + id/textSwitcher" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: textAlignment = "center" android: align = "true" android: layout_centerVertical = "true" android: inAnimation = "@ android: anim/slide_in_left" android: outAnimation = "@ android: anim/slide_out_right" android: onClick = "next"> </TextSwitcher> </RelativeLayout>
The layout File above defines a TextSwitcher, and specifies the animation effect for text switching for this text switch. Then, the TextSwitcher can work normally as long as the Activity sets ViewFactory for this TextSwitcher.
The following is the Activity code:
/*** TextSwitcher practice. * @ author peter. **/public class MainActivity extends Activity {private TextSwitcher textSwitcher; // The text String [] strs = new String [] {"one", "two ", "three"}; private int curStr; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); textSwitcher = (TextSwitcher) findViewById (R. id. textSwitcher); textSwitcher. setFactory (new ViewFactory () {@ Overridepublic View makeView () {TextView TV = new TextView (MainActivity. this); TV. setTextSize (40); // font color: Red TV. setTextColor (Color. MAGENTA); return TV ;}}); // call the next method to display the next string next (null);} // event processing function, the control displays the next string public void next (View source) {textSwitcher. setText (strs [curStr ++ % strs. length]) ;}}