android textView实现跑马灯效果

布局文件:
<?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" >
    <!-- android:focusable="true"也可以不要,因为自定义的view里面已经设置了位true,当两者都设置时,自定义的优先级更高 ,
<span style="white-space:pre">	</span>如果不自定义View,仅仅靠xml属性设置,后面的textView焦点会被第一个抢夺,从而只有第一个实现跑马灯效果,后面的不能实现-->
	<com.example.exercise.MarqueeTextView
	    android:id="@+id/text1"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:singleLine="true"
	    android:focusableInTouchMode="true"
	    android:focusable="true"
	    android:ellipsize="marquee"
	    android:text="@string/marquee_text"
	    />
	
	<com.example.exercise.MarqueeTextView
	    android:id="@+id/text2"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:singleLine="true"
	    android:focusableInTouchMode="true"
	    
	    android:ellipsize="marquee"
	    android:text="@string/marquee_text"
	    />
</LinearLayout>


自定义的textView<span style="white-space:pre">	</span><pre name="code" class="java">package com.example.exercise;

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

public class MarqueeTextView extends TextView {

	public MarqueeTextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public MarqueeTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public MarqueeTextView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	
<span style="white-space:pre">	</span>//主要就是这个方法让此控件位聚焦状态
	@Override
	public boolean isFocused() {
		// TODO Auto-generated method stub
		return true;
	}

}

<pre name="code" class="java">package com.example.exercise;

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

public class MarqueeTextActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.marquee_textview);
	}
}

在配置文件中配置入口:

        <activity android:name="com.example.exercise.MarqueeTextActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>





<span style="white-space:pre">
</span>



</pre><pre name="code" class="java">

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章