实现效果主要代码段(运行在模拟器上就能看到效果)
android:ellipsize="marquee"
android:singleLine="true"
android:marqueeRepeatLimit="marquee_forever"
通过此段代码则可实现跑马灯效果,然而此处,仅有这段代码只能实现一处跑马灯效果,并不能实现多行都能进行。
看下面这段代码以及一些基本设置
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_light"
android:orientation="vertical" >
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_view_long"
android:textColor="@android:color/holo_orange_dark"
android:textSize="20sp"
android:maxEms="5" <!--设置显示的字数-->
android:ellipsize="marquee" <!-- 设置显示效果 -->
android:focusable="true" <!--确定是否能进行焦点操作,通过点击将效果移至点击处-->
android:singleLine="true" <!-- 设置单行运行 -->
android:marqueeRepeatLimit="marquee_forever" <!-- 设置次数 -->
android:focusableInTouchMode="true"
/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_view_long"
android:textColor="@android:color/holo_orange_dark"
android:textSize="20sp"
android:maxEms="5"
android:ellipsize="marquee"
android:focusable="true"
android:singleLine="true"
android:marqueeRepeatLimit="marquee_forever"
android:focusableInTouchMode="true"
/>
</LinearLayout>
<!-- ----------------------------------------------------- -->
若是想进行多行同时进行跑马灯效果,则可通过下面代码实现
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- com.example.ui.widget.Marquee_Test1 由包名+类名组成 -->
<com.example.ui.widget.Marquee_Test1 android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_view_long"
android:textColor="@android:color/holo_orange_dark"
android:textSize="20sp"
android:maxEms="5"
android:ellipsize="marquee"
android:singleLine="true"
android:marqueeRepeatLimit="marquee_forever"
/>
<com.example.ui.widget.Marquee_Test1 android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_view_long"
android:textColor="@android:color/holo_blue_bright"
android:textSize="20sp"
android:layout_marginTop="20dp"
android:maxEms="5"
android:ellipsize="marquee"
android:singleLine="true"
android:marqueeRepeatLimit="marquee_forever"
/>
</LinearLayout>
Java文件://通过继承Textview来重写isFocused实现,
public class Marquee_Test1 extends TextView{
public Marquee_Test1(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public Marquee_Test1(Context context, AttributeSet attrs) {
super(context, attrs);
}
public Marquee_Test1(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean isFocused() {
// TODO Auto-generated method stub
return true; //始终返回true则可实现滚动效果了
}
}
最后关于注册实现则同其他实现方法相同,需另建类继承activity父类实现,Marquee_Test1类只是负责多行滚动效果处理。