First step: Create a new project, Marqueetextview
First, in order to observe the marquee effect, the text that will be displayed is likely to be long written. In the Strings.xml folder, the
<stringname="hello_world">hello_world</string>
Revision changed to
<stringname="hello_world">我的代码很长,真的很长,不行你看看,实际上是骗你的,逗比,hiahia~~~~</string>
By default, the display text wraps automatically! In order to achieve a marquee effect, you first want to prevent it from wrapping itself. Implemented by using the Singleline Property!
Android:singleline= "true"
If the goal is only to achieve the single-line text of the marquee effect, can only be achieved by three statements!
Android has a Ellipsize property,
Android:ellipsize= "Marquee"
Android:focusable= "true"
Android:focusableintouchmode= "true"
Through the above code can achieve single-line text of the marquee effect. However, if you want to achieve multi-line text of the marquee effect, the above code repeated writing, can not achieve the desired function! Because in the focusable attribute above, the focus has been defined on the first line! The follow-up was not focus!. Can be achieved by creating a new class Class!
In the SRC folder, create a new class named MarqueeText
PackageCom.example.marqueetextviewdemo1;ImportAndroid.content.Context;ImportAndroid.util.AttributeSet;ImportAndroid.widget.TextView; Public class marqueetext extends TextView{ Public MarqueeText(Context context) {Super(context);//TODO auto-generated constructor stub} Public MarqueeText(context context, AttributeSet attrs,intDefstyle) {Super(Context, attrs, Defstyle);//TODO auto-generated constructor stub} Public MarqueeText(context context, AttributeSet attrs) {Super(context, attrs);//TODO auto-generated constructor stub} Public Boolean isFocused() {return true; }}
The TextView in the Main.xml file is then modified to the package name +.+ class name.
This revision is meant to be all defined in this way, to get focus, and then achieve the effect of the marquee!
The results shown are as follows:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android realizes the marquee effect