ViewSwitcher 的作用简单来说就是:在两个视图间转换时显示动画
它的两个子类应该很熟悉,
ImageSwitcher:转换图片时增加动画效果;
TextSwitcher: 转换文字时增加动画效果;
布局:
<?xml version="1.0" encoding="utf-8"?> <ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/view_switcher" android:layout_width="match_parent" android:inAnimation="@android:anim/slide_in_left" android:outAnimation="@android:anim/slide_out_right" android:layout_height="match_parent" > <!-- 第一个视图 --> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/ne" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="下一张"/> </RelativeLayout> <!-- 第二个视图 --> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/up" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="上一张"/> <ImageView android:id="@+id/image" android:layout_toRightOf="@id/up" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:src="@drawable/ic_launcher"/> </RelativeLayout> </ViewSwitcher>
界面的切换
public class ViewSwitcherAct extends Activity{ private ViewSwitcher switcher; private Button upper,next; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.view_switcher); switcher = (ViewSwitcher) findViewById(R.id.view_switcher); upper = (Button) findViewById(R.id.up); next = (Button) findViewById(R.id.ne); //切换到第一个view switcher.setDisplayedChild(0); upper.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { switcher.showPrevious(); } }); next.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { switcher.showNext(); } }); } }
布局:
时间: 2024-12-12 17:25:10