The first kind: simpler, but strong limitations, it seems that can only run from right to left, and there is a requirement: the length of the font should be greater than the length of the control, otherwise there is no effect, the important code for the dark part, the specific code at the end of the article.
Introduction to important code
1. Android: Ellipsize = “marquee
2. Android :focusable= “true
3. The android: focusableInTouchMode = “true” could be obtained by touch
4. The android: marqueeRepeatLimit = “marquee_forever” set the number of repeat
5. Android :singleLine= true a singleLine is displayed
Second: use the timer to change the value of the padding to play a horselight effect, all directions can be, without the first requirement, the main role of the setPadding method, remember to turn off the timer at the end!
1. Layout file
2. Code to implement the function
Again, remember to turn off the timer!
The specific code is as follows:
The first method:
<TextView android:text=" what's happening in class? I don't know. Heckle first." android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:marqueeRepeatLimit="marquee_forever" android:singleLine="true" android:layout_width="200dp" android:layout_height="wrap_content" />Copy the code
The second way:
Layout file:
<TextView android:id="@+id/mytext" android:gravity="center_vertical" Android :text=" what happened in class? I don't know. Heckle first." android:layout_width="match_parent" android:layout_height="40dp" />Copy the code
Java file:
public class MainActivity extends Activity { private TextView mytext; private Handler handler; private Runnable runnable; private int theroll=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); marquee(); } private void initView() {// instantiate myText = (TextView) findViewById(R.I.D.myText); } private void marquee(){handler=new handler (); runnable=new Runnable() { @Override public void run() { if (theroll>80){ theroll=-80; } theroll=theroll+2; Mytext.setpadding (theroll,0,0,0); // set the following parameters to control the scrolling direction mytext.setpadding (theroll,0,0,0); handler.postDelayed(this,100); // Adjustable speed}}; handler.postDelayed(runnable,0); } @Override protected void onDestroy() { super.onDestroy(); Handler. RemoveCallbacks (Runnable); }}Copy the code