在某些APP經常看到一個條目循環滾動消息,這是怎么實現的呢?后來聽人說是TextSwitcher控件,借鑒他人,自己也來寫一寫,不為別的就是為了自己以后用着的時候方便些。廢話不多說,貌似全是廢話,開始。
1、布局
<TextSwitcher
android:singleLine="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ts" ></>
2、代碼
implements ViewSwitcher.ViewFactory
ts.setFactory(this);//一定不要忘了
實現方法 makeView(){
//文字大小 顏色
TextView textview=new TextView();
textView.setTextSize(20);
textviewv.setTextColor(Color.BLUE);
}
1、模擬滾動的數據
String[] text={
"條目一","條目二","條目三"
};
2、設置滾動的時間間隔,將text設置到TextSwitcher上
Handler handler = new Handler();
Runnable runnable = new Runnable() {
public void run() {
id++;
if (id == text.length) {
id = 0;//重置
}
ts.setText(text[id]);
handler.postDelayed(this, 3000);
}
};
3、設置滾動的動畫,上下滾動
ts.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.fade_in));
ts.setOutAnimation();
1、xml文件fade_in
就是補間動畫
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="100%p"
android:toXDelta="0%p"
android:duration="1200"
/>
4、設置點擊事件
ts.setOnClickListener(new View.OnClickListener(){
public void onClick(Veiw v){
switch(id){
case 0:
break;
}
}
});
