2014-11-17 3 views
0

기존 TextView가 있습니다 (코드 이름은 입니다.) 멋진 텍스트로 텍스트를 변경하고 싶습니다.Android - TextSwitcher - TextView 변경

텍스트를 변경하는 애니메이션을 만드는 유일한 방법은 TextSwitcher를 사용하는 것입니다.

이 코드 사용하려고

:

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

내가 무엇을 할 수

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"); 

을 그리고이 코드에서 예외가 발생? 애니메이션으로 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을 가지고 있다는 것입니다.

당신은 당신의 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; 
    } 
}); 

에 사용자 지정 Factory 설정을 시도 할 수 있습니다 당신은 XML 파일에서 인용 TextView을 제거 할 수 있으며 quoteSwitcher.addView(quote);를 제거합니다. 자세한 내용은이 blog을 확인하십시오.

0

귀하의 코드에있는 문제는 이미 부모보기가있는 XML에서 textview를 추가한다는 것입니다. 다음 코드를 사용하십시오. 그것에서 나는 기존 부모가없는 런타임에 textview를 만들고 문제를 해결할 textWatcher에 추가하고 있습니다.