这几天在学习Android程序启动页加引导页以及导航点的实现,我们可以通过ViewPager来实现整个过程,下面我们来看看传统的方法实现的过程,然后通过与“Android史上最简单的引导页导航点实现方式”对比一下,可以发现代码量以及复杂程度都大大的减轻了呢。
1.传统方式实现:首先导航点的实现我们需要到github上面去下载相应的依赖库,来完成我们需要的效果。如下:
这里我们就用CirclePageIndicator小圆点来实现导航点的效果。
Activity主布局文件:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/id_viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white" />
android:id="@+id/indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="8dp"
android:padding="10dp" />
然后就是引导页的三个布局,这里只展示一个就行了,因为几乎都是一样的。
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/bg_boot_page_one"/>
接下来就是我们的activity的文件了
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import com.viewpagerindicator.CirclePageIndicator;
import java.util.ArrayList;
import java.util.List;
import Adapter.ViewPagerAdapter;
public class ViewPagerActivity extends Activity{
private CirclePageIndicator circlePageIndicator;
ViewPager viewPager;
ViewPagerAdapter mpagerAdapter;
private List views= new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewpager);
viewPager= (ViewPager) findViewById(R.id.id_viewpager);
circlePageIndicator= (CirclePageIndicator) findViewById(R.id.indicator);
LayoutInflater minflater=LayoutInflater.from(this);
View view02=minflater.inflate(R.layout.viewpager02,null);
View view03=minflater.inflate(R.layout.viewpager03,null);
View view04=minflater.inflate(R.layout.viewpager04,null);
views.add(view02);
views.add(view03);
views.add(view04);
mpagerAdapter=new ViewPagerAdapter(views);
viewPager.setAdapter(mpagerAdapter);
circlePageIndicator.setViewPager(viewPager);
}
}
效果就不展示了,大家在APP启动程序中都见过,下面来见证我们的史上最简单的引导页导航点实现方式。
说白了,就是把导航点和引导页合二为一,我展示一张图片大家就全明白了。
然后主布局文件就变成了这样
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:id="@+id/id_viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white" />
其他的文件就不用我写了吧,因为只要一点点的变动即可。
看到了吧,导航点本身就是图片的一部分,这样做起来其实效果是差不多的,也可以实现对我们的引导页进行导航的一个效果,不用下载什么依赖库也不用在布局文件和activity里写那么多代码了,是不是很简单呢。
转载时请注明出处及相应链接,本文永久地址:https://blog.yayuanzi.com/14129.html
微信打赏
支付宝打赏
感谢您对作者Joy的打赏,我们会更加努力! 如果您想成为作者,请点我