У меня есть существующий TextView (в коде его имя - quote), и я хочу изменить его текст с помощью фантазийной анимации.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" />
Постарайтесь проверить, не является ли cathwitchcher hash дочерним, затем вызвать quoteSwitcher.removeView() перед цитатойSwitcher.addView (quote). –
проверить http://www.learn-android-easily.com/2013/06/android-textswitcher.html –
не могли бы вы разместить свой xml ?? –