笔记6 TextView 跑马灯效果

来源:互联网 发布:一级域名免费注册 编辑:程序博客网 时间:2024/06/11 11:16
image - 想要获取图片通过src(图片不会变形) 想要设置背景用background(图片会变形)。

方法一:通过设置android:singleLine、android:ellipsis、android:focusable、android:focusableInTouchMode这四个属性来实现跑马灯效果

<TextView
    android:id="@+id/tv1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:text="@string/app_name1"/>


方法二:自定义TextView,重写isFocused()方法,使全部的textview都可以一直获取焦点。
代码如下:
public class MarQueeTextView extends TextView {
    public MarQueeTextView(Context context) {
        super(context);
    }

    public MarQueeTextView(Context contextAttributeSet attrs) {
        super(contextattrs);
    }

    public MarQueeTextView(Context contextAttributeSet attrs, int defStyleAttr) {
        super(contextattrsdefStyleAttr);
    }

    @Override
    public boolean isFocused() {
        return true;//可以一直获取焦点。
    }
}

布局文件如下:
<page.pay.third.com.marquee.MarQueeTextView
    android:id="@+id/tv1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:text="@string/app_name1"/>














0 0
原创粉丝点击