单个 activity 上的多个 TextSwitcher

Multiple TextSwitcher on single activity

我想一个接一个地 Activity 放置 3 个 TextSwitcher。 问题是只有第一个文本切换器工作正常。其余的什么也没显示。也没有抛出异常。当我删除第一个时,第二个开始正常工作。我假设只有一个 TextSwitcher 可以存在于单个 activity 中,但我找不到这方面的确认。或者也许我做错了什么,因为我是 Android 世界的新人。
这是我的部分观点:

    <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,我创建了以下方法:

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 对象 + Handler 来安排文本更改:

    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);
        }
    }
};

我认为您的问题是,由于您的布局设置方式,您实际上一次只能看到一个 TextSwitcher,这解释了为什么当您删除第一个时第二个开始工作.

假设您希望 TextSwitcher 都在一条水平线上,一种可能的布局如下:

<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>