首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓- TextSwitcher -变更TextView

安卓- TextSwitcher -变更TextView
EN

Stack Overflow用户
提问于 2014-11-17 06:58:03
回答 2查看 3.3K关注 0票数 1

我有一个现有的TextView (在代码中它的名字是-引号),我想用花哨的动画修改它的文本。

创建一个改变文本的动画的唯一方法似乎是使用TextSwitcher。

我试着使用以下代码:

代码语言:javascript
复制
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");

这段代码抛出一个异常:

代码语言:javascript
复制
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

我能做什么?我只想用动画改变TextView文本。

完整代码:

代码语言:javascript
复制
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:

代码语言:javascript
复制
<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" />

EN

回答 2

Stack Overflow用户

发布于 2014-11-17 07:14:22

您的问题是,引号TextView已经具有父级,即LinearLayout

您可以尝试将自定义Factory设置为TextSwitcher

代码语言:javascript
复制
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);。有关更多细节,请查看此博客

票数 2
EN

Stack Overflow用户

发布于 2014-11-17 07:25:20

代码中的问题是,您正在从xml中添加一个文本视图,该视图已经有一个父视图。使用以下代码。在它中,我在运行时创建了一个文本视图,没有现有的父视图,并将它添加到textWatcher中,这将解决您的问题。

代码语言:javascript
复制
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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26967166

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档