Android TextView Marquee Effect

Source: Internet
Author: User

First, the main points

Set four properties

Android:singleline= "true"
Android:ellipsize= "Marquee"
Android:focusable= "true"
Android:focusableintouchmode= "true"

Use directly in XML

<TextView        android:layout_width= "Wrap_content"        android:layout_height= "Wrap_content "        android:singleline=" true "        android:ellipsize=" marquee "        android:focusable = "true"        android:focusableintouchmode= "true"        android:text= "Life is an endless, no-break, The ruthless battle, the person who wants to be a merited called human, must always fight to the invisible enemy. "/>

Note: The Singleline property cannot be replaced with Maxllines

Second, complex layout

The marquee effect may not be achieved in complex layouts. For example, in the following layout, only the first TextView will have a marquee effect.

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:app= "Http://schemas.android.com/apk/res-auto"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" > <TextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/tv1"Android:singleline= "true"android:ellipsize= "Marquee"android:focusable= "true"Android:focusableintouchmode= "true"Android:text= "Life is an endless, non-stop, ruthless fight, and anyone who wants to be a merited called a man, must always fight against the invisible enemy." "/> <TextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_below= "@+id/tv1"Android:layout_margintop= "10DP"Android:singleline= "true"android:ellipsize= "Marquee"android:focusable= "true"Android:focusableintouchmode= "true"Android:text= "Life is an endless, non-stop, ruthless fight, and anyone who wants to be a merited called a man, must always fight against the invisible enemy." "/></relativelayout>

This time you need to customize the view to achieve the marquee effect

The custom Marqueetextview extents TextView overrides the IsFocused () method, which returns true

 Public classMarqueeTextextendsTextView { PublicMarqueeText (Context context) {Super(context); }     PublicMarqueeText (Context context, @Nullable AttributeSet attrs) {Super(context, attrs); }     PublicMarqueeText (context context, @Nullable AttributeSet attrs,intdefstyleattr) {        Super(context, attrs, defstyleattr); } @Override Public BooleanisFocused () {return true; }}

Use in layouts

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:app= "Http://schemas.android.com/apk/res-auto"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" > <Com.example.dhj.marqueedemo.View.MarqueeText android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/tv1"Android:singleline= "true"android:ellipsize= "Marquee"android:focusable= "true"Android:focusableintouchmode= "true"Android:text= "Life is an endless, non-stop, ruthless fight, and anyone who wants to be a merited called a man, must always fight against the invisible enemy." "/> <Com.example.dhj.marqueedemo.View.MarqueeText android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_below= "@+id/tv1"Android:layout_margintop= "10DP"Android:singleline= "true"android:ellipsize= "Marquee"android:focusable= "true"Android:focusableintouchmode= "true"Android:text= "Life is an endless, non-stop, ruthless fight, and anyone who wants to be a merited called a man, must always fight against the invisible enemy." "/></relativelayout>

Android TextView Marquee Effect

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.