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:
Use TruncateAt.START, TruncateAt.MIDDLE, TruncateAt.END to set the text truncation effect as follows:
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.
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:
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.
This article is contributed by Anonymous and text available under CC-SA-4.0