您的位置:首页 > 其它

安卓——实现TextViewde的跑马灯效果

2018-02-23 19:25 330 查看
1.xml布局文件com.example.lenovo.marqueetextview.MarqueeText是我们写的MarqueeText这个类的路径
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.lenovo.marqueetextview.MainActivity">

<com.example.lenovo.marqueetextview.MarqueeText
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
/>

<com.example.lenovo.marqueetextview.MarqueeText
android:id="@+id/textView2"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:singleLine="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
/>

</RelativeLayout>
2.Marquee继承TextView类
首先得把三个构造函数加上
然后覆写一下isFocused( )方法,让它一直返回true
这样xml文件里面的两个控件才会同时有跑马灯效果package com.example.lenovo.marqueetextview;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

/**
* Created by lenovo on 2018/2/23.
*/

public class MarqueeText extends TextView{
public MarqueeText(Context context){
super(context);
}

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

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

/*
我们写的这个类中让所有View有焦点
*/
@Override
public boolean isFocused(){
return true;
}
}
3.MainActivitypackage com.example.lenovo.marqueetextview;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: