添加依赖:
compile ‘com.astuetz:pagerslidingtabstrip:1.0.1’
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.pagerslidingtabstrip.MainActivity">
<com.astuetz.PagerSlidingTabStrip
android:id="@+id/ps"
android:layout_width="match_parent"
android:layout_height="wrap_content"></com.astuetz.PagerSlidingTabStrip>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/vp"
></android.support.v4.view.ViewPager>
</LinearLayout>
个性化设置
- 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 每个标签的背景,应该是一个StateListDrawablepstsShouldExpand 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
然后在activity中做一些处理。。。。。
package com.example.pagerslidingtabstrip;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import com.astuetz.PagerSlidingTabStrip;
public class MainActivity extends AppCompatActivity {
private ViewPager vp;
private PagerSlidingTabStrip ps;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vp = (ViewPager) findViewById(R.id.vp);
vp.setAdapter(new MyAdapter(getSupportFragmentManager()));
ps = (PagerSlidingTabStrip) findViewById(R.id.ps);
ps.setViewPager(vp);
}
class MyAdapter extends FragmentPagerAdapter {
String[] title = {"项目一", "项目二", "项目三"};
Fragment1 fragment1;
Fragment2 fragment2;
Fragment3 fragment3;
private Fragment1 fragment11;
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
fragment11 = new Fragment1();
return fragment11;
case 1:
fragment2 = new Fragment2();
return fragment2;
case 2:
fragment3 = new Fragment3();
return fragment3;
default:
return null;
}
}
@Override
public int getCount() {
return title.length;
}
@Override
public CharSequence getPageTitle(int position) {
return title[position];
}
}
}