Android PagerSlidingTabStrip 介绍及使用

来源:互联网 发布:下载全球直播软件 编辑:程序博客网 时间:2024/05/19 17:06

先看一下效果



github上已经有了这个开源项目,所以我们可以直接拿来用,很方便很实用。文章末尾我将放上资源,可以直接下载。

使用方法

1、下载Zip压缩文件

从 PagerSlidingTabStrip官网(https://github.com/astuetz/PagerSlidingTabStrip)上下载Zip文件,下载成功后解压缩Zip文件

2、导入library和sample项目

Import -> Android -> Existing Android Code Into Workspace -> Browse,选择你解压缩的library 目录即可,记住勾选 Copy projects into workset 

Android Studio 加入依赖在build.gradle

dependencies {    compile 'com.astuetz:pagerslidingtabstrip:1.0.1'}
3.在你的layout里加入PagerSlidingTabStrip控件,它通常要在ViewPager控件之上

<com.astuetz.PagerSlidingTabStrip    android:id="@+id/tabs"    android:layout_width="match_parent"    android:layout_height="48dip" />

4.在你的onCreate方法(或者onCreateView对于一个fragment),绑定PagerSlidingTabStrip控件到ViewPager上

// 初始化ViewPager并且添加适配器 ViewPager pager = (ViewPager) findViewById(R.id.pager); pager.setAdapter(new TestAdapter(getSupportFragmentManager())); //向ViewPager绑定PagerSlidingTabStrip   PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs); tabs.setViewPager(pager);


5.(可选的)如果你想在你的ViewPager用到onPageChangeListener监听方法,你应该如下设置,而不是直接用ViewPager设置

// 从上面继续 tabs.setOnPageChangeListener(mPageChangeListener);

个性化设置

为了让你的app不像另一个 Play Store上面的app,你可以添加这些属性来做出自己独具一格的应用。

  • pstsIndicatorColor Color of the sliding indicator  滑动条的颜色
  • pstsUnderlineColor Color of the full-width line on the bottom of the view  滑动条所在的那个全宽线的颜色
  • pstsDividerColor Color of the dividers between tabs   每个标签的分割线的颜色
  • pstsIndicatorHeightHeight of the sliding indicator       滑动条的高度
  • pstsUnderlineHeight Height of the full-width line on the bottom of the view    滑动条所在的那个全宽线的高度
  • pstsDividerPadding Top and bottom padding of the dividers   分割线底部和顶部的填充宽度
  • pstsTabPaddingLeftRight Left and right padding of each tab   每个标签左右填充宽度
  • pstsScrollOffset Scroll offset of the selected tab
  • pstsTabBackground Background drawable of each tab, should be a StateListDrawable  每个标签的背景,应该是一个StateListDrawable  
  • pstsShouldExpand If set to true, each tab is given the same weight, default false   如果设置为true,每个标签是相同的控件,均匀平分整个屏幕,默认是false
  • pstsTextAllCaps If true, all tab titles will be upper case, default true   如果为true,所有标签都是大写字母,默认为true

所有的属性都有他们自己的getter和setter方法来随时改变他们





0 0
原创粉丝点击