Android TextView text horizontal scrolling automatically (marquee)


widgets Article
Android TextView text horizontal scrolling automatically (marquee)

The text in TextView can be set with some special effects. The horizontal scrolling of the text is briefly described below. To scroll horizontally, you need to meet some conditions:

  1. Ellipsize is set to TruncateAt.MARQUEE;
  2. SingleLine is set to true;
  3. FocusableInTouchMode is set to true, and RequestFocus;
  4. The length of the text in the TextView is greater than the length of the TextView; "* This is a prerequisite! * 』
  5. The code example is as follows:
    Java code: 
  1. TextView textView = (TextView) findViewById(R.id.textView1);  
  2. textView.setEllipsize(TruncateAt.MARQUEE);  
  3. textView.setSingleLine();  
  4. textView.setFocusableInTouchMode(true);  
  5. textView.requestFocus();  

 Use TruncateAt.START, TruncateAt.MIDDLE, TruncateAt.END to set the text truncation effect as follows:



TextView requires the following points to achieve text scrolling:
1. The text length is longer than the display range: android: singleLine = "true"
2. The settings can be scrolled to, or display style: android: ellipsize = "marquee"
3. TextView only scrolls the hidden text after it gets focus, so you need to create a new class in the package that inherits TextView. Override the isFocused method. The default behavior of this method is that if the TextView gets focus, the method returns true, and if it loses focus it returns false. The marquee effect estimation also uses this method to determine whether it has the focus, so its return value is always set to true.

The following is transferred from others:

Java language AlwaysMarqueeTextView class

public  class AlwaysMarqueeTextView extends TextView {

public AlwaysMarqueeTextView(Context context{
super(context);
}

public AlwaysMarqueeTextView(Context context, AttributeSet attrs{
super(context, attrs);
}

public AlwaysMarqueeTextView(Context context, AttributeSet attrs, int defStyle{
super(context, attrs, defStyle);
}

@Override
public  boolean isFocused() {
return  true;
}

Adding such an AlwaysMarqueeTextView to the layout XML file, this joining method is also just learned.

XML languagelayout.xml
<com.examples.AlwaysMarqueeTextView
android:id= “@ + id / AMTV1 ″
android:layout_width= “ fill_parent ”
android:layout_height= “ wrap_content ”
android:lines= “ 1 ″
android:focusable= “true”
android:focusableInTouchMode= “ true ”
android:scrollHorizontally= “ true ”
android:marqueeRepeatLimit= “ marquee_forever ”
android:ellipsize= “ marquee ”
android:background= “ @android: color / transparent ”
/>

The ellipsize property
sets how the control should be displayed when the text is too long. The following values ​​are set: "start"-the ellipsis is displayed at the beginning; "end"-the ellipsis is displayed at the end; "middle"-the ellipsis is displayed in the middle; "marquee"-displayed in the manner of a marquee (animation moves laterally) )

The marqueeRepeatLimit attribute
sets the number of repeat scrolls when the marquee is specified by ellipsize. When set to marquee_forever, it means unlimited times.

The focusable property
guessed whether it should be able to get focus, and the same focusableInTouchMode should be able to get focus when sliding.

The problem of combining View:

XML language combined View
<LinearLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:orientation = "vertical"
android:gravity = "center_vertical"
android:background = "@ drawable / f_background"
android:layout_width = “Fill_parent”
android:focusable = “true”
android:layout_height = “50px”   >
<TextView
android:id = “@ + id / info_text”
android:focusable = “true”
android:layout_width = “fill_parent”
android:layout_height = "Wrap_content"
android:text = "test marquee .." "
android:textColor = " @ color / black "
android:singleLine = “true”
android:ellipsize = “marquee”
android:marqueeRepeatLimit = "3"
android:textSize = "18sp"
/>
<TextView
android:id = "@ + id / date_text"
android:layout_width = "fill_parent"
android:layout_height = “wrap_content”
android:layout_gravity = “bottom”
android:textColor = “@ color / gray”
android:text = “2010/05/28 ″
android:textSize = “ 12sp ”
/>
</ LinearLayout>

In the above example, the two TextViews are combined into one View. Because the LinearLayout is set to focusable and the TextView cannot get the focus, so the TextView's marquee effect cannot be displayed, even if you also set the TextView. It  android:focusable="true" is useless. This When you need to use the attribute addStatesFromChildren, set this attribute in LinearLayout,然后设置TextView的focusable= "true" 就可以了.关于 addStatesFromChildren的说明:

Sets whether this ViewGroup's drawable states also include its children's drawable states.


widgets Related Articles

widgets Contribution

This article is contributed by Anonymous and text available under CC-SA-4.0