Android Custom TextView to achieve unlimited marquee effect

Source: Internet
Author: User

In doing app development, some titles need to achieve the effect of the TextView, if the use of the system with the implementation of this effect, just add the following code in the TextView properties:

Android:ellipsize= "marquee"//Happy Lantern effect
android:marqueerepeatlimit= "Marquee_forever"//Unrestricted uninterrupted display
Android:singleline= "true"//single line display

But there is a fatal drawback, that is, this state of the horse can only be in the focus of the TextView, it will roll, for the actual application of the app development applications is not practical, because it can not always be in the acquisition focus state.

In order to be the happy lights in any case can run up to achieve the desired effect, we need to customize a TextView inheritance TextView, and rewrite the Isfocuse () method, so that it always return true, so that the effect of the horse can always run up.

The code is as follows:

Package Zm.marqueetextview;

Import Android.content.Context;
Import Android.util.AttributeSet;
Import Android.widget.TextView;

public class Marqueetextview extends TextView {

Public Marqueetextview {
Super (context);
}

Public Marqueetextview (context context, AttributeSet Attrs) {
Super (context, attrs);
}

Public Marqueetextview (context context, AttributeSet attrs, int defstyle) {
Super (context, attrs, Defstyle);
}

@Override
public Boolean isfocused () {
return true;
}
}

You can then refer directly to the layout file:

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:padding= "15DP" >

<zm.marqueetextview.marqueetextview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:singleline= "true"
Android:ellipsize= "Marquee"
android:marqueerepeatlimit= "Marquee_forever"
Android:text= "This is a custom TextView with a marquee effect this is a custom TextView with a marquee effect"/>

</RelativeLayout>

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.