Textswitcher integrates Viewswitcher, so it has the same features as Viewswitcher: You can use animation effects when switching view components. Similar to Imageswitcher, the use of Textswitcher also requires setting up a viewfactory. Unlike Imageswitcher, the Viewfactory Makeview () method required by Textswitcher must return a TextView component.
<textswitcher is a bit like the TextView, which can be used to display text content, except that the textswitcher effect is more dazzling, which specifies the animation effect when the text is toggled. >
Not much to say, directly on 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 " > <!--define a textswitcher, and develop a text toggle animation effect-- <textswitcher android:id= "@+id/textswitcher" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:textalignment= " Center " android:layout_centerhorizontal=" true " android:layout_centervertical=" true " Android: Inanimation= "@android: Anim/slide_in_left" android:outanimation= "@android: Anim/slide_out_right" Android:onclick= "Next" > </TextSwitcher></RelativeLayout>
a textswitcher is defined in the layout file above and an animated effect is specified for the text toggle, and the activity is then set viewfactory for the Textswitcher. The Textswitcher can work properly.
Here is the activity code:
/** * Textswitcher Practice. * @author Peter. * */public class Mainactivity extends Activity { private textswitcher textswitcher; Text to display string[] STRs = new string[] {"One", "a", " 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 Magenta tv.settextcolor (color.magenta); return TV;}}); Call the next method to display the next string next (null); } event handler that controls the display of the next string public void Next (View source) {textswitcher.settext (strs[curstr++% strs.length]);}}
Function and usage of text switcher (Textswitcher)