Android TextView realizes the marquee

Source: Internet
Author: User

TextView realize the effect of the marquee:
Example one:

This example can be solved to a textview realize the effect of the marquee, but can not solve all the textview to achieve the effect of the marquee.

 <  textview  android:layout_width  = "Wrap_content"   = "Wrap_content"   = "true"   Android: Ellipsize  = "marquee"   android:focusable  = "true"   android:focusableintouchmode< /span>= "true"   Android:text  = "The function of this project is to make this single line of a long and long single row of TextView realize the effect of the marquee ... " />  

But this method can not give two textview at the same time to achieve the effect of the textview when there are two.
The reason is that textview default isfocused () method can only be implemented focused for an object.
The workaround is to create a new class Marqueetextview inherit TextView and let his isfocused () method return True. (Note: Subclass Marqueetextview needs to implement all 3 constructors of the parent class, otherwise there will be a problem).

 PackageCom.example.textviewhorseracelamp;ImportAndroid.content.Context;ImportAndroid.util.AttributeSet;ImportAndroid.widget.TextView; Public classMarqueetextviewextendsTextView { PublicMarqueetextview (Context context) {Super(context); //TODO auto-generated Constructor stub    }     PublicMarqueetextview (context context, AttributeSet attrs,intDefstyle) {        Super(context, attrs, Defstyle); //TODO auto-generated Constructor stub    }     PublicMarqueetextview (Context context, AttributeSet attrs) {Super(context, attrs); //TODO auto-generated Constructor stub} @Override Public BooleanisFocused () {return true; }}
Marqueetextview.java
<Relativelayoutxmlns: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"    >    <Com.example.textviewhorseracelamp.MarqueeTextViewAndroid:id= "@+id/textview1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:singleline= "true"android:ellipsize= "Marquee"android:focusable= "true"Android:focusableintouchmode= "true"Android:text= "The purpose of this project is to make this single line of long, long, separate lines of TextView realize the effect of the marquee ... " />        <Com.example.textviewhorseracelamp.MarqueeTextViewAndroid:id= "@+id/textview2"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_margintop= "20DP"Android:singleline= "true"android:ellipsize= "Marquee"android:focusable= "true"Android:focusableintouchmode= "true"Android:text= "The purpose of this project is to make this single line of long, long, separate lines of TextView realize the effect of the marquee ... " /></Relativelayout>
Activity_main.xml

Effect:

Added: pixels
PX cannot be scaled according to resolution
Dp,sp,dip can be scaled based on resolution (now DP as standard)
SP more used in the text zoom

Android TextView realizes the marquee

Related Article

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.