下载PagerSlidingTabStrip https://github.com/astuetz/PagerSlidingTabStrip
主界面:
public class MainActivity extends FragmentActivity { private ViewPager viewPager;
private PagerSlidingTabStrip tab;
private String[] Titles={"页面一","页面二","页面三","页面四"};
private DisplayMetrics dm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setAdapter(new TestAdapter(getSupportFragmentManager()));
tab=(PagerSlidingTabStrip) findViewById(R.id.tabStrip);
tab.setViewPager(viewPager); dm = getResources().getDisplayMetrics();
// 设置填充满屏幕
tab.setShouldExpand(true);
// 设置分割线
tab.setDividerColor(Color.TRANSPARENT);
// 设置底部线的高度
tab.setUnderlineHeight((int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 1, dm));
// 设置Indicator的高度
tab.setIndicatorHeight((int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 5, dm));
// 标题文字的大小
tab.setTextSize((int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP, 18, dm));
// Indicator的颜色
tab.setIndicatorColor(Color.parseColor("#569844"));
// 设置选中文字的颜色
tab.setSelectedTextColor(Color.parseColor("#569844"));
// 点击的背景色
tab.setTabBackground(0);
} class TestAdapter extends FragmentPagerAdapter{ public TestAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
} @Override
public Fragment getItem(int position) {
// TODO Auto-generated method stub
PagerFragment fragment=new PagerFragment();
Bundle bundle=new Bundle();
bundle.putString("TITLE", Titles[position]);
fragment.setArguments(bundle);
return fragment;
} @Override
public int getCount() {
// TODO Auto-generated method stub
return Titles.length;
}
@Override
public CharSequence getPageTitle(int position) {
// TODO Auto-generated method stub
return Titles[position];
} } }
fragment类:
public class PagerFragment extends Fragment { @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
RelativeLayout layout=new RelativeLayout(getActivity());
LayoutParams params=new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
layout.setLayoutParams(params);
TextView text=new TextView(getActivity());
text.setLayoutParams(params);
text.setGravity(Gravity.CENTER);
Bundle bundle=getArguments();
String title=bundle.getString("TITLE");
text.setText(title);
layout.addView(text);
return layout;
}
}
attr:
<?xml version="1.0" encoding="utf-8"?>
<resources> <declare-styleable name="PagerSlidingTabStrip">
<attr name="pstsIndicatorColor" format="color" />
<attr name="pstsUnderlineColor" format="color" />
<attr name="pstsDividerColor" format="color" />
<attr name="pstsIndicatorHeight" format="dimension" />
<attr name="pstsUnderlineHeight" format="dimension" />
<attr name="pstsDividerPadding" format="dimension" />
<attr name="pstsTabPaddingLeftRight" format="dimension" />
<attr name="pstsScrollOffset" format="dimension" />
<attr name="pstsTabBackground" format="reference" />
<attr name="pstsShouldExpand" format="boolean" />
<attr name="pstsTextAllCaps" format="boolean" />
</declare-styleable> </resources>