首先简单看下ViewAnimator,因为其他3个都是复写了他,限制为最多2个控件切换而已。
默认的情况下显示第一个布局。红色的部分默认都不显示
<ViewAnimator
android:id="@+id/animator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffc0c0c0"
android:animateFirstView="false"
>
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<ProgressBar
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
</ViewAnimator>
animator = (ViewAnimator)findViewById(R.id.animator);
//想展示那个布局就调用下边的方法即可
animator.setDisplayedChild(1);
==================
<span style="font-size:18px;">TextSwitcher的使用,很简单</span>
<span style="font-size:18px;"> <TextSwitcher
android:id="@+id/switcher"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</span>
<span style="font-size:18px;"> // Get the TextSwitcher view from the layout
mSwitcher = (TextSwitcher) findViewById(R.id.switcher);
// Set the factory used to create TextViews to switch between.
mSwitcher.setFactory(mFactory);
/*
* Set the in and out animations. Using the fade_in/out animations
* provided by the framework.
*/
Animation in = AnimationUtils.loadAnimation(this,
android.R.anim.fade_in);
Animation out = AnimationUtils.loadAnimation(this,
android.R.anim.fade_out);
mSwitcher.setInAnimation(in);
mSwitcher.setOutAnimation(out);</span>
那么如何给textswitcher赋值了?有两种方法
// Set the initial text without an animation
mSwitcher.setCurrentText(“不带动画的”);
第二种:mSwitcher.setText(“带动画的”);
上边的factory如下:需要返回一个textview,负责会报错的。
<span style="font-size:18px;"> private ViewFactory mFactory = new ViewFactory() {
@Override
public View makeView() {
// Create a new TextView
TextView t = new TextView(MainActivity.this);
t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
t.setTextAppearance(MainActivity.this, android.R.style.TextAppearance_Large);
return t;
}
};</span>
当然如果你不想用ViewFactory初始化的话,可以直接在xml代码里添加2个textview也可以的。如下:
<span style="font-size:18px;"> <TextSwitcher
android:id="@+id/switcher"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</TextSwitcher></span>
ImageSwitcher的使用和上边的基本一样:
简单说下如何赋值:
mImageSwitcher=(ImageSwitcher) findViewById(R.id.imageSwitcher1);
mImageSwitcher.setFactory(imageFactory);
mImageSwitcher.setImageResource(R.drawable.ic_launcher);
private ViewFactory imageFactory=new ViewFactory() {
@Override
public View makeView() {
ImageView iv=new ImageView(MainActivity.this);
return iv;
}
};
ViewSwitcher的使用和上一样,个人建议还是直接在xml代码里添加2个控件比较方便。。
如果要用ViewFactory创建的话,如下处理:
mViewSwitcher =(ViewSwitcher) findViewById(R.id.viewSwitcher1);
mViewSwitcher.setFactory(viewFactory);
//如下获取到2个切换的view,然后修改里边需要修改的控件的值。
Button btn0= (Button) mViewSwitcher.getChildAt(0).findViewById(R.id.button1);
btn0.setText("000000");
Button btn1= (Button) mViewSwitcher.getChildAt(1).findViewById(R.id.button1);
btn1.setText("1111111");
//如何切换这2个view?下边2个方法
mViewSwitcher.showNext();
mViewSwitcher.showPrevious();
private ViewFactory viewFactory=new ViewFactory() {
@Override
public View makeView() {//这个里边有个button按钮和一个Textview不写了
View view=LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null);
return view;
}
};
至于源码,自己下载去看哦,在adt如下目录:adt\sdk\samples\android-21\ui\TextSwitcher