前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >视图切换(ViewSwitcher)使用

视图切换(ViewSwitcher)使用

作者头像
李小白是一只喵
发布2020-04-23 15:19:23
1.5K0
发布2020-04-23 15:19:23
举报
文章被收录于专栏:算法微时光算法微时光

目录

ViewSwitcher

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

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

值得注意的是ViewSwitcher最多只能有2个view. ViewSwitcher的addView函数的代码如下:

代码语言:javascript
复制
/**
 * {@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时,就会报错.

使用:

代码语言:javascript
复制
    <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文件:

代码语言:javascript
复制
<?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>

代码实现:

代码语言:javascript
复制
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();
            }
        });
    }
}

运行效果:

参考

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 目录
  • ViewSwitcher
  • 使用实例
  • 参考
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档