本案例实现一个仿微信的引导界面,功能是在程序第一次下载启动后出现一个引导界面,以后再次打开不会出现了(只会在程序第一次启动的时出现)
实现步骤:
第一步;
写四个子布局page01.xml,page02.xml,page03.xml,page04.xml,作为启动以次显示的四个界面这里写了一个,其它三个类似
page01.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#8A2BE2"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="page01"
android:textSize="40sp" >
</TextView>
</LinearLayout>
page04.xml第四个界面可以加入一个按钮,点击进入主界面:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFF00" >
<TextView
android:id="@+id/textview_page04"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="page04"
android:textSize="40sp" >
</TextView>
<Button
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:text="点我进入"
android:onClick="startMain"
android:textSize="35sp" />
</RelativeLayout>
第二步主界面中
<RelativeLayout 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"
tools:context=".Gudie_Activity" >
<android.support.v4.view.ViewPager
android:id="@+id/vp_viewpage"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</android.support.v4.view.ViewPager>
</RelativeLayout>
package com.example.traffic;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
public class Gudie_Activity extends Activity {
private ViewPager viewpager;
private List<View> children;
private String filename = "config";
private String firstRunning = "true";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gudie_);
SharedPreferences sp = getSharedPreferences(filename, MODE_PRIVATE);
String config = sp.getString(firstRunning, null);
if(config != null){
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
viewpager = (ViewPager) findViewById(R.id.vp_viewpage);
children = new ArrayList<View>();
children.add(LayoutInflater.from(this).inflate(R.layout.page01, null));
children.add(getLayoutInflater().inflate(R.layout.page02, null));
children.add(getLayoutInflater().inflate(R.layout.page03, null));
children.add(getLayoutInflater().inflate(R.layout.page04, null));
viewpager.setAdapter(new InnerPagerAdapter());
}
public class InnerPagerAdapter extends PagerAdapter{
@Override
public int getCount() {
//获取子级布局的数量
return children.size();//返回viewpage容器中子级布局的个数,
}
@Override
public boolean isViewFromObject(View view, Object object) {
// 判断某个View对象是否为当前被添加到ViewPager容器中的对象
return view == object;
}
//下面两个方法是PageAdapter中必须实现的非抽象方法
@Override
public Object instantiateItem(ViewGroup container, int position) {
// 实例化ViewPager容器中指定的position位置需要显示的View对象
View view = children.get(position);
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
//在ViewPager中移除指定的position位置的View对象
container.removeView(children.get(position));
}
}
public void startMain(View v){
SharedPreferences sp = getSharedPreferences(filename, MODE_PRIVATE);
Editor editor = sp.edit();
editor.putString(firstRunning, "false");
editor.commit();
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
finish();//引导界面的Activity就会退出,回退栈中也会清除该Activity,点击回退按钮也不会回退到引导界面
}
}