字面意思可能不太好理解,那就来个动图吧
以上效果是来自第三方库 MarqueeView
如果想了解更多请到 GitHub 搜索 跑马灯
使用系统的 TextView 实现
- 自定义一个TextView
貌似也可以不用自定义 ,直接在 xml 中设置
isFocused = true
, 待验证?
public class MarqueeTextView extends AppCompatTextView {
public MarqueeTextView(Context context) {
super(context);
}
public MarqueeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MarqueeTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean isFocused(){
return true;
}
}
- 在 xml 中设置相应的属性
<com.xxxx.xxxx.xxx.widget.MarqueeTextView
android:id="@+id/post_content_tv"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true" />
主要的属性介绍
android:layout_width="200dp"
表示超过这个范围的文字才会滚动
android:focusable="true"
和android:focusableInTouchMode="true"
需要设置让其 获取焦点 方可滚动
android:singleLine="true"
设置让其单行一直滚动
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"