2014-11-17 186 views
0

我有一個現有的TextView(代碼它的名字 - 報價),我想改變它的文本與花哨的動畫。的Android - TextSwitcher - 更改的TextView

這似乎是創建一個文本改變動畫的唯一方式是使用TextSwitcher。

我嘗試使用此代碼:

quoteSwitcher = (TextSwitcher)findViewById(R.id.quote_switcher); 

     quoteSwitcher.addView(quote); 

     Animation in = AnimationUtils.loadAnimation(this, 
       android.R.anim.fade_in); 
     Animation out = AnimationUtils.loadAnimation(this, 
       android.R.anim.fade_out); 

     quoteSwitcher.setInAnimation(in); 
     quoteSwitcher.setOutAnimation(out); 

     quoteSwitcher.setText("Example text"); 

而這個代碼拋出一個異常:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 

我能做些什麼?我只想用動畫改變TextView文字。

全碼:

protected void initWidgets() { 
    quote = (TextView)findViewById(R.id.quote); // Афоризм 

    /* Начальное значение афоризма */ 
    quote.setText(getRandomQuote()); 

    /* Плавная анимация смены афоризмов */ 
    quoteSwitcher = (TextSwitcher)findViewById(R.id.quote_switcher); 

    quoteSwitcher.removeAllViewsInLayout(); 

    quoteSwitcher.addView(quote); 

    Animation in = AnimationUtils.loadAnimation(this, 
      android.R.anim.fade_in); 
    Animation out = AnimationUtils.loadAnimation(this, 
      android.R.anim.fade_out); 

    quoteSwitcher.setInAnimation(in); 
    quoteSwitcher.setOutAnimation(out); 

    quoteSwitcher.setText(getRandomQuote()); 
} 

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
android:layout_height="match_parent" tools:context=".MainActivity" 
android:orientation="vertical" 
android:focusableInTouchMode="false" 
android:id="@+id/main_layout"> 

<TextSwitcher 
    android:id="@+id/quote_switcher" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"></TextSwitcher> 

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/logotype_layout"> 

    <ImageView 
     android:layout_width="200dp" 
     android:layout_height="200dp" 
     android:id="@+id/logotype" 
     android:layout_gravity="center_horizontal" 
     android:src="@drawable/logotype" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="20dp" /> 
</RelativeLayout> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="@string/main_logotext" 
    android:id="@+id/logotext" 
    android:layout_gravity="center_horizontal" 
    android:layout_marginTop="20dp" 
    android:textSize="55sp" /> 

<TextView 
    android:fontFamily="sans-serif-thin" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="@string/main_quote_0" 
    android:id="@+id/quote" 
    android:layout_gravity="center_horizontal" 
    android:layout_marginTop="10dp" 
    android:textSize="15sp" 
    android:textStyle="italic" /> 

+0

嘗試檢查quoteSwitcher哈希子然後在quoteSwitcher.addView(quote)之前調用quoteSwitcher.removeView()。 –

+0

檢查http://www.learn-android-easily.com/2013/06/android-textswitcher.html –

+0

你可以發佈你的XML? –

回答

1

您的問題是報價TextView已經有父即LinearLayout

你可以嘗試定製Factory設置爲您TextSwitcher

quoteSwitcher.setFactory(new ViewFactory() { 
public View makeView() { 
     // TODO Auto-generated method stub 
     // create new textView and set the properties like clolr, size etc 
     TextView myText = new TextView(MainActivity.this); 
     myText.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL); 
     myText.setTextSize(36); 
     myText.setTextColor(Color.BLUE); 
     return myText; 
    } 
}); 

,你可以從XML文件中刪除該帖TextView也刪除quoteSwitcher.addView(quote);。有關更多詳細信息,請查看blog

0

在你的代碼的問題是,您要添加從XML一個TextView它已經有一個父視圖。使用下面的代碼。在這裏面我創建在沒有現有父運行一個TextView並將其添加到textWatcher這將解決您的問題。

mSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher); 
    mSwitcher.setFactory(new ViewFactory() { 
     public View makeView() { 
      // create new textView and set the properties like clolr, size etc 
      TextView myText = new TextView(MainActivity.this); 
      myText.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL); 
      myText.setTextSize(36); 
      myText.setTextColor(Color.BLUE); 
      return myText; 
     } 
    }); 

    // Declare the in and out animations and initialize them 
    Animation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left); 
    Animation out = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right); 

    // set the animation type of textSwitcher 
    mSwitcher.setInAnimation(in); 
    mSwitcher.setOutAnimation(out);