ViewSwitcher的简单使用

遇到新的需求:

     在页面中点击下一步的时候再同一个activity中进行页面的切换显示

所以简单的时候ViewSwitcher就搞定了,源码:

Activity代码:

package com.gupiaobang.app;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ViewSwitcher;

public class RegisterActivity extends Activity{

	private ViewSwitcher vs_content;
	
    @Override 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_module_register_first);
	    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.public_title);  
        initView();
    }
    
    public void initView(){
    	vs_content = (ViewSwitcher) findViewById(R.id.vs_content);
		//使用者这个方法就可以进行要全面的切换了
		vs_content.setDisplayedChild(1); //显示第二个页面
			
    }
    
}
页面布局:

<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"
     >
    <!-- 注册界面第一个页面 -->
	 <ViewSwitcher 
	     android:id="@+id/vs_content"
	     android:layout_width="match_parent"
    	 android:layout_height="match_parent"
    	 android:inAnimation="@android:anim/slide_in_left"
         android:outAnimation="@android:anim/slide_out_right"
	     >
		 <!-- 将要切换显示的页面放入到ViewSwitcher中,然后在activity中就可以通过
				setDisplayedChild进行切花View的显示了
		 -->
	     <!-- 第一页 -->
	     <LinearLayout 
	        android:layout_width="match_parent"
    		android:layout_height="match_parent"
    		android:orientation="vertical"
	         >
		    <!-- 确认 -->
		    <Button 
		        android:id="@+id/r_next"
		        android:layout_width="fill_parent"
	    	 	android:layout_height="wrap_content"
	    	 	android:layout_marginTop="@dimen/margin_small"
	    	 	android:paddingTop="@dimen/padding_small"
    			android:paddingBottom="@dimen/padding_small"
	    	 	style="@style/text_red_big"
	    	 	android:background="@drawable/button_press"
	    	 	android:text="@string/r_next"
		        />
	     </LinearLayout>
	     <!-- 第二页 -->
	     <LinearLayout 
	        android:layout_width="match_parent"
    		android:layout_height="match_parent"
    		android:orientation="vertical"
	         >
			    <!-- 确认 -->
			    <Button 
			        android:id="@+id/r_finish"
			        android:layout_width="fill_parent"
		    	 	android:layout_height="wrap_content"
		    	 	android:paddingTop="@dimen/padding_small"
    				android:paddingBottom="@dimen/padding_small"
		    	 	android:layout_marginTop="@dimen/margin_max"
		    	 	style="@style/text_red_big"
		    	 	android:background="@drawable/button_press"
		    	 	android:text="@string/r_finish"
			        />
		  </LinearLayout>
	 </ViewSwitcher>
	
</LinearLayout>



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。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值