[Android] marquee effect (Text scrolling)

Source: Internet
Author: User

The effect of a running horse lamp is the effect of scrolling in the control when the text exceeds the space that the control can hold.

To achieve this effect, you must add the following in the layout file:

android:singleLine=”true”android:ellipsize=”marquee”android:focusableInTouchMode=”true”android:focusable=”true”

Note: layout_width = "" must be written as a fixed value. It cannot be wrap_content or fill_parent, and it must be longer than the text length. You can also set the number of scrolling times. Android: marqueerepeatlimit = "";
Android: marqueerepeatlimit = "marquee_forever" indicates rolling.

However, this type of Marquee can only be rolled when the control gets the focus. To keep the content in the control rolling, you must customize the control and rewrite the three methods:

package cn.etzmico.marqueetest;import android.content.Context;import android.graphics.Rect;import android.util.AttributeSet;import android.widget.Button;public class MarqueeButton extends Button {public MarqueeButton(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub}@Overrideprotected void onFocusChanged(boolean focused, int direction,Rect previouslyFocusedRect) {// TODO Auto-generated method stubif(focused)super.onFocusChanged(focused, direction, previouslyFocusedRect);}@Overridepublic void onWindowFocusChanged(boolean hasWindowFocus) {// TODO Auto-generated method stubif(hasWindowFocus)super.onWindowFocusChanged(hasWindowFocus);}@Overridepublic boolean isFocused() {return true;}}

The following figure shows how to use this control in the layout file:

<cn.easymobi.application.memorytest.MarqueeButtonandroid:layout_width=”216dip”android:layout_height=”wrap_content”android:id=”@+id/btSecond”android:background=”@drawable/button_test2″android:layout_marginTop=”15dip”android:text=”@string/calculate”android:ellipsize=”marquee”android:gravity=”center”android:textColor=”@color/white”android:textStyle=”bold”android:focusable=”true”android:marqueeRepeatLimit=”marquee_forever”android:focusableInTouchMode=”true”android:scrollHorizontally=”true”android:singleLine=”true”android:paddingLeft=”50dip”android:paddingRight=”50dip”android:textSize=”20dip”/>

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.