Android mobile guard-custom controls (get focus TextView) and androidtextview
URL: http://www.cnblogs.com/wuyudong/p/5906735.html.
This article will implement the text marquee effect in textview under the title bar, that is, to roll a line of text horizontally, the effect is as follows:
The implementation code is as follows:
<! -- Android: ellipsize = "end": location where the omitted vertex is added --> <! -- If you want the text to have a running lantern effect, you must focus on it. --> <! -- Android: marqueeRepeatLimit = "marquee_forever" always rolling properties --> <! -- Customize the control to achieve the rolling effect (in fact, it is to refresh the original TextView, so that it can always get the focus) --> <TextView android: text = "this is a running horse lamp, this is a running horse lamp, this is a running horse lamp, this is a running horse lamp, this is a running horse lamp, "android: layout_width =" match_parent "android: layout_height =" wrap_content "android: ellipsize =" marquee "android: focusable = "true" android: focusableInTouchMode = "true" android: marqueeRepeatLimit = "marquee_forever" android: padding = "5dp" android: textColor = "#000" android: singleLine = "true"/>
If you also need this effect in other places, it is troublesome to copy the code. Here we use a custom control to implement the scrolling effect (in fact, it is to refresh the original TextView and keep it focused)
Create a new view for the custom control file
Create a FocusTextView class
Add code:
Package com. wuyudong. mobilesafe. view; import android. content. context; import android. util. attributeSet; import android. widget. textView; /*** @ author wuyudong * can get the custom TextView of the Focus **/public class FocusTextView extends TextView {// use the control public FocusTextView (Context context) created in java code) {super (context) ;}// called by the system (with attributes + Context constructor) public FocusTextView (context, AttributeSet attrs) {super (context, attrs );} // The system calls the public FocusTextView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle);} // Override the method for getting the focus @ Override public boolean isFocused () {// return super. isFocused (); return true ;}}
The layout code is replaced:
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <TextView style = "@ style/TitleStyle" android: text = "function list"/> <! -- Android: ellipsize = "end": location where the omitted vertex is added --> <! -- If you want the text to have a running lantern effect, you must focus on it. --> <! -- Android: marqueeRepeatLimit = "marquee_forever" always rolling properties --> <! -- Customize the control to achieve the scrolling effect (in fact, it is to refresh the original TextView and keep it focused) --> <! -- <TextView android: text = "this is a running horse lamp, this is a running horse lamp," android: layout_width = "match_parent" android: layout_height = "wrap_content" android: ellipsize = "marquee" android: focusable = "true" android: focusableInTouchMode = "true" android: marqueeRepeatLimit = "marquee_forever" android: padding = "5dp" android: textColor = "#000" android: singleLine = "true"/> --> <com. wuyudong. mobilesafe. view. focusTextView android: text = "this is a running horse lamp, this is a running horse lamp," android: layout_width = "match_parent" android: layout_height = "wrap_content" android: ellipsize = "marquee" android: marqueeRepeatLimit = "marquee_forever" android: padding = "5dp" android: textColor = "#000" android: singleLine = "true"> </com. wuyudong. mobilesafe. view. focusTextView> </LinearLayout>
To sum up custom controls
Custom Control Writing Process
Create a TextView that can get the focus by default.
1. Create a class to inherit from TextView and FocusTextView
2. Rewrite its constructor
Public class FocusTextView extends TextView {// use the control created in java code: public FocusTextView (Context context) {super (context );} // The system calls public FocusTextView (Context context, AttributeSet attrs) {super (context, attrs );} // The system calls the public FocusTextView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle);} // Override the method for getting the focus @ Override public boolean isFocused () {// return super. isFocused (); return true ;}}
3. Change the isFocus method on the original TextView to get the focus by default.
// Override the method for getting the focus @ Override public boolean isFocused () {// return super. isFocused (); return true ;}
4. Use Process
Obtains the full path name of the current class, which exists as a tag in xml. The usage of other attributes is the same as that of TextView.