ViewSwitcher使用例子

android.widget.ViewSwitcher是ViewAnimator的子类,用于在两个Viewzhij
摘要由CSDN通过智能技术生成

android.widget.ViewSwitcher是ViewAnimator的子类,用于在两个View之间切换,但每次只能显示一个View。

ViewSwitcher的addView函数的代码如下:

    /**
     * {@inheritDoc}
     *
     * @throws IllegalStateException if this switcher already contains two children
     */
    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        if (getChildCount() >= 2) {
            throw new IllegalStateException("Can't add more than 2 views to a ViewSwitcher");
        }
        super.addView(child, index, params);
    }

   

可以看出,若View的数量超过两个,会抛出异常: java.lang.IllegalStateException,打印 "Can't add more than 2 views to a ViewSwitcher" 。你可以使用ViewSwitcher的factory创建View或添加自己创建的View。

下面用一个例子介绍一下ViewSwitcher的用法。

布局文件:activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              tools:context=".MainActivity" >


    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

        <Button
                android:id="@+id/prev"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="previous" />

        <Button
                and
  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
viewSwitcher是一个布局容器,可以用于实现切换不同的布局视图。它能够在两个或多个视图之间切换,并且可以设置切换的动画效果。 下面是一个简单的例子,演示如何使用viewSwitcher实现切换两个不同的布局视图: 1. 定义两个布局文件: 布局1:activity_main.xml ``` <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFC0CB"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is layout 1" android:textSize="30sp" android:layout_centerInParent="true"/> </RelativeLayout> ``` 布局2:activity_second.xml ``` <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout2" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#008B8B"> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is layout 2" android:textSize="30sp" android:layout_centerInParent="true"/> </RelativeLayout> ``` 2. 在activity_main.xml中添加viewSwitcher布局容器: ``` <?xml version="1.0" encoding="utf-8"?> <ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/viewSwitcher" android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/activity_main"/> <include layout="@layout/activity_second"/> </ViewSwitcher> ``` 3. 在Activity中实现切换功能: ``` public class MainActivity extends AppCompatActivity { private ViewSwitcher viewSwitcher; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewSwitcher = (ViewSwitcher) findViewById(R.id.viewSwitcher); // 设置切换动画效果 viewSwitcher.setInAnimation(this, android.R.anim.fade_in); viewSwitcher.setOutAnimation(this, android.R.anim.fade_out); // 点击布局1切换到布局2 findViewById(R.id.layout1).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { viewSwitcher.showNext(); } }); // 点击布局2切换到布局1 findViewById(R.id.layout2).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { viewSwitcher.showPrevious(); } }); } } ``` 在Activity中获取到viewSwitcher实例,并设置切换动画效果。然后通过监听布局1和布局2的点击事件来实现切换功能。当点击布局1时,调用viewSwitcher.showNext()方法切换到布局2;当点击布局2时,调用viewSwitcher.showPrevious()方法切换到布局1。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值