android 用户引导页实现

来源:互联网 发布:社交网络 评论音轨 编辑:程序博客网 时间:2024/05/21 06:37

用户引导页实现


我们通过ViewPage来实现,首先在主布局中放入ViewPage:

1.<?xml version="1.0" encoding="utf-8"?>  2.<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  3.    xmlns:tools="http://schemas.android.com/tools"  4.    android:layout_width="match_parent"  5.    android:layout_height="match_parent"  6.  7.    tools:context="com.example.myapplication.MainActivity">  8.  9.    <android.support.v4.view.ViewPager  10.        android:id="@+id/view_page"  11.        android:layout_width="match_parent"  12.        android:layout_height="match_parent"></android.support.v4.view.ViewPager>  13.  14.  15.</RelativeLayout>  


这里ViewPage需要填满父控件,另外我们需要创建一个布局文件tab01:

<?xml version="1.0" encoding="utf-8"?>  <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"    android:background="@mipmap/guide_image1"      tools:context="com.example.myapplication.MainActivity">          <Button          android:id="@+id/button"          android:layout_width="wrap_content"         android:layout_height="wrap_content"          android:layout_alignParentBottom="true"          android:layout_centerHorizontal="true"          android:text="点击进入"         android:visibility="gone"         android:layout_marginBottom="20dp"/>    </RelativeLayout>


最后是JAVA文件


1.package com.example.myapplication;  2.  3.import android.app.Activity;  4.import android.net.Uri;  5.import android.support.v4.view.PagerAdapter;  6.import android.support.v4.view.ViewPager;  7.import android.support.v7.app.AppCompatActivity;  8.import android.os.Bundle;  9.import android.util.Log;  10.import android.view.LayoutInflater;  11.import android.view.View;  12.import android.view.ViewGroup;  13.import android.widget.Button;  14.import android.widget.ImageView;  15.import android.widget.Toast;  16.  17.import com.google.android.gms.appindexing.Action;  18.import com.google.android.gms.appindexing.AppIndex;  19.import com.google.android.gms.common.api.GoogleApiClient;  20.  21.import java.util.ArrayList;  22.import java.util.List;  23.  24.public class MainActivity extends Activity {  25.  26.  27.    private ViewPager view_page;  28.  29.    //滑动页面的数量  30.    private List<View> tabViews;  31.  32.  33.  34.    @Override  35.    protected void onCreate(Bundle savedInstanceState) {  36.        super.onCreate(savedInstanceState);  37.        setContentView(R.layout.activity_main);  38.        tabViews = new ArrayList<>();  39.        //初始化数据  40.        initData();  41.  42.        view_page = (ViewPager) findViewById(R.id.view_page);  43.        //设置适配器  44.        view_page.setAdapter(new PagerAdapter() {  45.            @Override  46.            public int getCount() {  47.                return tabViews.size();//  48.            }  49.  50.            @Override  51.            public boolean isViewFromObject(View view, Object object) {  52.                return view == object;  53.            }  54.  55.            @Override  56.            public Object instantiateItem(ViewGroup container, int position) {  57.                container.addView(tabViews.get(position));  58.                //滑到最后一个页面,显示button  59.                if(position == tabViews.size()-1)  60.                {  61.                   Button button= (Button) tabViews.get(position).findViewById(R.id.button);  62.                    button.setVisibility(View.VISIBLE);  63.                    button.setOnClickListener(new View.OnClickListener() {  64.                        @Override  65.                        public void onClick(View view) {  66.                            Toast.makeText(MainActivity.this,"你要的操作",Toast.LENGTH_LONG).show();  67.                        }  68.                    });  69.                }  70.                return tabViews.get(position);  71.            }  72.  73.            @Override  74.            public void destroyItem(ViewGroup container, int position, Object object) {  75.                container.removeView(tabViews.get(position));  76.            }  77.        });  78.  79.  80.  81.  82.    }  83.  84.    private void initData() {  85.        //引入一个布局,三个共用,设置不同的背景图片  86.        LayoutInflater tabs = LayoutInflater.from(MainActivity.this);  87.        View tab1 = tabs.inflate(R.layout.tab_01, null);  88.        tab1.setBackgroundResource(R.mipmap.guide1);  89.  90.        View tab2 = tabs.inflate(R.layout.tab_01, null);  91.        tab2.setBackgroundResource(R.mipmap.guide2);  92.        View tab3 = tabs.inflate(R.layout.tab_01, null);  93.  94.        tab3.setBackgroundResource(R.mipmap.guide3);  95.  96.        //添加到列表里  97.        tabViews.add(tab1);  98.        tabViews.add(tab2);  99.        tabViews.add(tab3);  100.  101.    }  102.  103.  104.  105.  106.}  


OK了 最后试着运行下就好了




原创粉丝点击