视图切换(ViewSwitcher)使用

作者: Lee_5566 | 来源:发表于2019-10-22 20:16 被阅读0次
    image.png

    目录

    ViewSwitcher

    ViewSwitcher顾名思义. ViewSwitcher主要应用场景之一:比如在一个布局文件中,根据业务需求,需要在两个View间切换,在任意一个时刻,只能显示一个View.

    ViewSwitcher本身继承了 FrameLayout,因此可以将多个View 层叠在一起,每次只显示一个组件。当程序控制从一个View切换到另一个View时, ViewSwitcher支持指定动画效果。

    值得注意的是ViewSwitcher最多只能有2个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超过2时,就会报错.

    使用:

        <ViewSwitcher
            android:id="@+id/viewswitcher"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="251dp"
            android:layout_marginBottom="145dp" >
    
            <ImageView
                android:layout_width="400dp"
                android:layout_height="400dp"
                android:src="@drawable/p001" />
    
            <ImageView
                android:layout_width="400dp"
                android:layout_height="400dp"
                android:src="@drawable/p002" />
    
        </ViewSwitcher>
    

    使用实例

    activity_main.xml文件:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <LinearLayout
            android:id="@+id/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
                android:id="@+id/next"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="next" />
        </LinearLayout>
    
        <ViewSwitcher
            android:id="@+id/viewswitcher"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="251dp"
            android:layout_marginBottom="145dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/linearLayout">
    
            <ImageView
                android:layout_width="400dp"
                android:layout_height="400dp"
                android:src="@drawable/p001" />
    
            <ImageView
                android:layout_width="400dp"
                android:layout_height="400dp"
                android:src="@drawable/p002" />
    
        </ViewSwitcher>
    
    </android.support.constraint.ConstraintLayout>
    

    代码实现:

    package com.example.user.viewsw;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.animation.Animation;
    import android.view.animation.AnimationUtils;
    import android.widget.Button;
    import android.widget.ViewSwitcher;
    
    public class MainActivity extends AppCompatActivity {
        Button buttonPrev, buttonNext;
        ViewSwitcher viewSwitcher;
        Animation slide_in_left, slide_out_right;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // 获取按钮
            buttonPrev = (Button) findViewById(R.id.prev);
            buttonNext = (Button) findViewById(R.id.next);
    
            // 获取ViewSwitcher
            viewSwitcher = (ViewSwitcher) findViewById(R.id.viewswitcher);
    
            // 载入动画效果
            slide_in_left = AnimationUtils.loadAnimation(this,
                    android.R.anim.slide_in_left);
            slide_out_right = AnimationUtils.loadAnimation(this,
                    android.R.anim.slide_out_right);
    
            // 设定动画效果
            viewSwitcher.setInAnimation(slide_in_left);
            viewSwitcher.setOutAnimation(slide_out_right);
    
            buttonPrev.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    viewSwitcher.showPrevious();
                }
            });
    
            buttonNext.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    viewSwitcher.showNext();
                }
            });
        }
    }
    
    

    运行效果:


    image.png
    image.png

    参考

    Android零基础入门第54节:视图切换组件ViewSwitcher
    android使用ViewSwitcher实现视图切换

    相关文章

      网友评论

        本文标题:视图切换(ViewSwitcher)使用

        本文链接:https://www.haomeiwen.com/subject/jtxcvctx.html