TextSwitcher 字面理解是文字交換器,是ViewSwitcher的子類,從ViewSwitcher來看,是View交換器,TextSwitcher繼承自ViewSwitcher,顯然是交換TextView。java
效果圖:
![](http://static.javashuo.com/static/loading.gif)
android
應用分爲三步:
1.獲得 TextSwitcher 實例對象
TextSwitcher switcher = (TextSwitcher) findViewById(R.id.textSwitcher);
2.爲switcher指定ViewSwitcher.ViewFactory工廠,該工廠會產生出轉換時須要的View
switcher.setFactory(this);
3.爲switcher設定顯示的內容,該方法執行,就會切換到下個View
switcher.setText(String.valueOf(new Random().nextInt()));
其中 要實現ViewSwitcher.ViewFactory中的makeView()方法
// 重寫 ViewSwitcher.ViewFactory 的 makeView()方法,返回一個 View,TextSwitcher 交換時使用
@Override
public View makeView() {
TextView textView = new TextView(this);
textView.setTextSize(36);
return textView;
}
若是不適用ViewSwitcher.ViewFactory,也能夠使用下面的方式代替
//若是不用switcher.setFactory()方法設置轉換時的View,也能夠調用兩次switcher.addView(view,index,params);
//其中view爲要切換的View,index爲索引,params是添加時的寬,高參數
// TextView textView1 = new TextView(this);
// textView1.setTextSize(36);
// textView1.setTextColor(Color.RED);
// TextView textView2 = new TextView(this);
// textView2.setTextSize(36);
// textView2.setTextColor(Color.YELLOW);
// switcher.addView(textView1, 0,new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
// switcher.addView(textView2, 1,new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
代碼:app
- package com.zhou.activity;
-
- import java.util.Random;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.animation.Animation;
- import android.view.animation.AnimationUtils;
- import android.widget.Button;
- import android.widget.TextSwitcher;
- import android.widget.TextView;
- import android.widget.ViewSwitcher;
-
- public class TextSwitcherActivity extends Activity implements ViewSwitcher.ViewFactory{
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- this.setContentView(R.layout.textswithcer);
- //設置標題
- setTitle("文字轉換器");
- //取得文字轉換器
- final TextSwitcher switcher = (TextSwitcher) findViewById(R.id.textSwitcher);
- // 指定轉換器的 ViewSwitcher.ViewFactory,ViewSwitcher.ViewFactory會爲TextSwitcher提供轉換的View
- switcher.setFactory(this);
-
- //若是不用switcher.setFactory()方法設置轉換時的View,也能夠調用兩次switcher.addView(view,index,params);
- //其中view爲要切換的View,index爲索引,params是添加時的寬,高參數
- // TextView textView1 = new TextView(this);
- // textView1.setTextSize(36);
- // textView1.setTextColor(Color.RED);
- // TextView textView2 = new TextView(this);
- // textView2.setTextSize(36);
- // textView2.setTextColor(Color.YELLOW);
- // switcher.addView(textView1, 0,new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
- // switcher.addView(textView2, 1,new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
-
- // 設置轉換時的淡入和淡出動畫效果(可選)
- Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
- Animation out = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
- switcher.setInAnimation(in);
- switcher.setOutAnimation(out);
-
- // 單擊一次按鈕改變一次文字
- Button btnChange = (Button) this.findViewById(R.id.btnChange);
- btnChange.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- //爲TextSwitcher設置顯示內容,執行一次switcher.setText()方法,就會切換到下一個View
- switcher.setText(String.valueOf(new Random().nextInt()));
- }
- });
- }
- // 重寫 ViewSwitcher.ViewFactory 的 makeView()方法,返回一個 View,TextSwitcher 交換時使用
- @Override
- public View makeView() {
- TextView textView = new TextView(this);
- textView.setTextSize(36);
- return textView;
- }
- }
ps: 關於如何更改TextSwitcher字體顏色的問題dom
這個問題咋一看簡單,可是沒有門路的話,半天也解決不了,我也遇到了這個問題,個人TextSwitcher默認顏色是灰色,和個人背景圖顏色差很少了,想改個顏色,可是找了好久也找不到解決辦法。ide
弄了一小時才找到解決的辦法(還要感謝某羣的羣主「飛雪無情」)給個人提示~~字體
現特貼出解決方案(其實很簡單,可是一時想不到的話,也會讓人很抓狂):動畫
public View makeView() {
TextView tv = new TextView(this);
tv.setTextSize(36);
tv.setTextColor(Color.BLACK);
return tv;
}
修改TextSwitcher的makeView()中的 tv.setTextColor(Color.BLACK); 就行了。this