【问题标题】:Android - TextSwitcher - Change TextViewAndroid - TextSwitcher - 更改 TextView
【发布时间】:2014-11-17 06:58:03
【问题描述】:

我有一个现有的 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" />

【问题讨论】:

标签: android text view switch-statement


【解决方案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

【讨论】:

    【解决方案2】:

    您的代码中的问题是您正在从您的 xml 添加一个文本视图,该文本视图已经有一个父视图。使用以下代码。在其中,我在运行时创建一个没有现有父级的 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-09
      • 1970-01-01
      相关资源
      最近更新 更多