链接地址:https://github.com/astuetz/PagerSlidingTabStrip
下载PagerSlidingTabStrip-master
将com.astuetz包,res下的下的资源复制进工程
布局文件:activity_main.xml
<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:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="sunny.example.lianpagerslidingtabstrip.MainActivity" xmlns:app="http://schemas.android.com/apk/res/sunny.example.lianpagerslidingtabstrip"><com.astuetz.PagerSlidingTabStripandroid:id="@+id/pagerSlidingTabStrip"android:layout_width="match_parent"android:layout_height="50dp"app:pstsShouldExpand="true"app:pstsIndicatorHeight="2dp"/><android.support.v4.view.ViewPagerandroid:id="@+id/viewPager"android:layout_below="@+id/pagerSlidingTabStrip"android:layout_width="fill_parent"android:layout_height="fill_parent"/></RelativeLayout>
<!--
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
pstsIndicatorHeight:Height of the sliding indicator TAB底部滑动横线的高度
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
pstsShouldExpand: If set to true, each tab is given the same weight, default false
pstsTextAllCaps: If true, all tab titles will be upper case, default true 默认Tab上的TextView大写字母-->
android:layout_below="@id/tabs"一定要记得写上,是让ViewPager控件在tabs之下。还有注释部分的属性可以使用。使用格式:app:
在MainActivity.java中使用:
public class MainActivity extends FragmentActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ViewPager viewPager = (ViewPager)findViewById(R.id.viewPager);viewPager.setAdapter(new myPagerAdapter(getSupportFragmentManager()));PagerSlidingTabStrip tabs = (PagerSlidingTabStrip)findViewById(R.id.pagerSlidingTabStrip);//PagerSlidingTabStrip绑定ViewPagertabs.setViewPager(viewPager);}class myPagerAdapter extends FragmentPagerAdapter{String[] title = {"item1","item2","item3"};BlueFragment blueFragment;GreenFragment greenFragment;OrangeFragment orangeFragment;public myPagerAdapter(FragmentManager fm) {super(fm);// TODO Auto-generated constructor stub}@Overridepublic Fragment getItem(int position) {// TODO Auto-generated method stubswitch(position){case 0:blueFragment = new BlueFragment();return blueFragment;case 1:greenFragment = new GreenFragment();return greenFragment;case 2:orangeFragment = new OrangeFragment();return orangeFragment;default:return null;}}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn title.length;}@Override public CharSequence getPageTitle(int position) { return title[position]; } }}
//PagerSlidingTabStrip绑定ViewPager
tabs.setViewPager(viewPager);
其中每个page是一个Fragment。
完整代码上传在:https://github.com/HiSunny/PagerSlidingTabStrip