作者:sunhuan | 来源:互联网 | 2023-05-19 08:43
TextView有时候显示的文字会很长,需要像跑马灯那样水平滚动显示。这方面也有开源库,但是如果需求不复杂的话其实用TextView的一些属性就可以实现实现过程只需2步Tex
TextView有时候显示的文字会很长,需要像跑马灯那样水平滚动显示。这方面也有开源库,但是如果需求不复杂的话其实用TextView的一些属性就可以实现
实现过程只需2步
"match_parent"
android:layout_hljs-string">"match_parent"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizOntally="true"
android:singleLine="true"
android:text="@string/buy_plan_top_tip"
android:textColor="@color/black_secondary"
android:textSize="11sp" />
- 由于TextView需要获取到焦点才会滚动起来,实际项目中可能TextView并不能总是获取焦点,所以需要对TextView进行简单的改造
public class MarqueeTextView extends android.support.v7.widget.AppCompatTextView {
public MarqueeTextView(Context context) {
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;
}
}
OK,通过以上简单2步我们就实现了跑马灯效果啦!希望对大家有用!