您的位置:首页 > 移动开发 > Android开发

Android自定义不需要焦点的TextView以实现跑马灯效果

2016-11-28 11:15 861 查看
    使用原生的TextView实现“跑马灯”效果,需要特别注意其是否获取到了焦点(特别是程序当中有多个需要赋值的

Textview或进度条之类的需要改变其值的View);TextView只有在获得了焦点的时候才有跑马灯的效果,这样有时候并

不能达到我们所要求的效果。我们可以自定义一个永远已经获取焦点的TextView。

    具体代码如下所示。

package com.example.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewDebug.ExportedProperty;
import android.widget.TextView;

public class AmMarqueeTextView extends TextView {

public AmMarqueeTextView(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}

public AmMarqueeTextView(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

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

@Override
@ExportedProperty(category = "focus")
public boolean isFocused() {
return true;
}

}
   
将自定义的AmMarqueeTextView放到布局文件中,如下所示。

<cn.net.ample.music.view.AmMarqueeTextView
android:id="@+id/tv_music_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text="@string/tip_music_name"
android:textColor="@color/grass_green"
android:textSize="@dimen/current_theme_text_size" />
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android TextView 跑马灯