首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >单活动的多个TextSwitcher

单活动的多个TextSwitcher
EN

Stack Overflow用户
提问于 2015-10-09 18:53:09
回答 1查看 435关注 0票数 1

我想把3 TextSwitcher放在一个又一个的活动中。问题是只有第一个文本开关才能正常工作。其余的什么都没有。此外,也没有引发任何异常。当我删除第一个,第二个开始工作良好。我假设在单个活动中只能存在一个TextSwitcher,但我找不到这方面的证实。或许我做错了什么,因为我是Android世界的新手。

这是我的观点之一:

代码语言:javascript
复制
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="bottom|center_horizontal">
        <TextSwitcher
            android:id="@+id/textSwitcher1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </TextSwitcher>
        <TextSwitcher
            android:id="@+id/textSwitcher2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </TextSwitcher>
        <TextSwitcher
            android:id="@+id/textSwitcher3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </TextSwitcher>
    </LinearLayout>
</FrameLayout>

要设置TextSwitcher,我创建了以下方法:

代码语言:javascript
复制
private TextSwitcher InitializeTextSwitcher(int textSwitcherId) {
    TextSwitcher ts = (TextSwitcher)findViewById(textSwitcherId);

    ts.setFactory(new ViewSwitcher.ViewFactory() {
        @Override
        public View makeView() {
            TextView myText = new TextView(MainActivity.this);
            myText.setGravity(Gravity.CENTER_HORIZONTAL);
            myText.setTextSize(48);
            myText.setTextColor(Color.WHITE);
            return myText;
        }
    });

    Animation in = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
    Animation out = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right);

    ts.setInAnimation(in);
    ts.setOutAnimation(out);

    return ts;
}

最后,我使用Runnable object + Handler调度文本更改:

代码语言:javascript
复制
    Runnable r=new Runnable() {
    // Override the run Method
    public void run() {
        // TODO Auto-generated method stub
        try
        {
            if(timeElapsed == timeStep) {
                textSwitcher1.setText(textToShow[0]);
            }else if (timeElapsed == 2*timeStep){
                textSwitcher2.setText(textToShow[1]);
            }else if(timeElapsed == 3*timeStep){
                textSwitcher3.setText(textToShow[2]);
            }else if (timeElapsed == 4*timeStep){
                 //Do something else
            }
        }
        finally
        {
            timeElapsed += timeStep;
            mHandler.postDelayed(this, timeStep);
        }
    }
};
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-09 19:19:24

我相信您的问题是,由于布局的设置方式,您每次只看到一个TextSwitcher,这就解释了为什么删除第一个布局时第二个开始工作。

假设您希望TextSwitcher的全部位于一条水平线上,一个可能的布局如下所示:

代码语言:javascript
复制
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_gravity="bottom|center_horizontal">
    <TextSwitcher
        android:id="@+id/textSwitcher1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">
    </TextSwitcher>
    <TextSwitcher
        android:id="@+id/textSwitcher2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">
    </TextSwitcher>
    <TextSwitcher
        android:id="@+id/textSwitcher3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">
    </TextSwitcher>
</LinearLayout>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33045288

复制
相关文章

相似问题

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