Android - TextSwitcher - 更改 TextView

标签 android text view switch-statement

我有一个现有的 TextView(在代码中它的名称是 quote),我想用精美的动画更改它的文本。

似乎创建文本更改动画的唯一方法是使用 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" />

最佳答案

您的问题是引用 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 .

关于Android - TextSwitcher - 更改 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26967166/

相关文章:

php - 仅删除 txt 文件中的 LF

SQL Server View |内联 View 扩展指南

android - 从保存的实例恢复 fragment 时如何防止 editText 自动填充文本?

Android 以编程方式设置 LinearLayout 背景

python - 元素树 iter() 正在跳过随机元素

ios - 隐藏 subview 会在 View 上留下微弱的阴影

Android:使用系统图像查看器查看多个图像

Android 无法解析 rxjava 中的订阅方法

android - 安卓网络服务器

java - Java 程序中硬编码文本字符串的替代做法?