Functions and usage of TextSwitcher,

Source: Internet
Author: User

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]) ;}}

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.