我想把3 TextSwitcher放在一个又一个的活动中。问题是只有第一个文本开关才能正常工作。其余的什么都没有。此外,也没有引发任何异常。当我删除第一个,第二个开始工作良好。我假设在单个活动中只能存在一个TextSwitcher,但我找不到这方面的证实。或许我做错了什么,因为我是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 object + 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);
}
}
};发布于 2015-10-09 19:19:24
我相信您的问题是,由于布局的设置方式,您每次只看到一个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>https://stackoverflow.com/questions/33045288
复制相似问题