Github地址:https://github.com/astuetz/PagerSlidingTabStrip
1,Include the library
dependencies { compile 'com.astuetz:pagerslidingtabstrip:1.0.1' }
2,
<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" tools:context=".MainActivity" xmlns:app="http://schemas.android.com/apk/res/com.example.viewfragment"> <com.astuetz.PagerSlidingTabStrip android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="48dip" app:pstsShouldExpand="true" /> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_below="@id/tabs" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </RelativeLayout>
3,使用和代码设置属性,
private void initView() { get_record_viewpager = (ViewPager) this .findViewById(R.id.get_record_viewpager); get_record_tab = (PagerSlidingTabStrip) this .findViewById(R.id.get_record_tab); dm = getResources().getDisplayMetrics(); monthGetRecordFragment = new MonthGetRecordFragment(); totalGetRecordFragment = new TotalGetRecordFragment(); fragmentList.add(monthGetRecordFragment); fragmentList.add(totalGetRecordFragment); pagerAdapter = new GetRecordsPagerAdapter(getSupportFragmentManager(), fragmentList); get_record_viewpager.setAdapter(pagerAdapter); get_record_tab.setViewPager(get_record_viewpager); setTabsValue(); }
/** * 对PagerSlidingTabStrip的各项属性进行赋值。 */ private void setTabsValue() { // 设置Tab是自动填充满屏幕的 get_record_tab.setShouldExpand(true); // 设置Tab的分割线是透明的 get_record_tab.setDividerColor(Color.TRANSPARENT); // 设置Tab底部线的高度 get_record_tab.setUnderlineHeight((int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, 1, dm)); // 设置Tab Indicator的高度 get_record_tab.setIndicatorHeight((int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, 2, dm)); // 设置Tab标题文字的大小 get_record_tab.setTextSize((int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_SP, 15, dm)); // 设置Tab标题默认的颜色 get_record_tab.setTextColor(getResources().getColor( R.color.get_record_text_unselected_color)); // 设置选中Tab标题的颜色 get_record_tab.setSelectedTextColor(getResources().getColor( R.color.get_record_text_selected_color)); // 设置Tab底部线的颜色 get_record_tab.setUnderlineColor(getResources().getColor( R.color.get_record_line_unselected_color)); // 设置Tab Indicator的颜色 get_record_tab.setIndicatorColor(getResources().getColor( R.color.get_record_line_selected_color)); // 取消点击Tab时的背景色 // get_record_tab.setTabBackground(getResources().getColor(R.color.tab_pressed_hover)); }
或者XMl 中设置属性
个性化设置
为了让你的app不像另一个 Play Store上面的app,你可以添加这些属性来做出自己独具一格的应用。
-
pstsIndicatorColorColor of the sliding indicator 滑动条的颜色 -
pstsUnderlineColorColor of the full-width line on the bottom of the view 滑动条所在的那个全宽线的颜色 -
pstsDividerColorColor of the dividers between tabs 每个标签的分割线的颜色 -
pstsIndicatorHeightHeight of the sliding indicator 滑动条的高度 -
pstsUnderlineHeightHeight of the full-width line on the bottom of the view 滑动条所在的那个全宽线的高度 -
pstsDividerPaddingTop and bottom padding of the dividers 分割线底部和顶部的填充宽度 -
pstsTabPaddingLeftRightLeft and right padding of each tab 每个标签左右填充宽度 -
pstsScrollOffsetScroll offset of the selected tab -
pstsTabBackgroundBackground drawable of each tab, should be a StateListDrawable 每个标签的背景,应该是一个StateListDrawable -
pstsShouldExpandIf set to true, each tab is given the same weight, default false 如果设置为true,每个标签是相同的控件,均匀平分整个屏幕,默认是false -
pstsTextAllCapsIf true, all tab titles will be upper case, default true 如果为true,所有标签都是大写字母,默认为true
GetRecordsPagerAdapter.java
package com.example.viewpagerdemo; import java.util.List; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; /** * 类说明: * * @author fuyanan * @date 2015-8-3 * @version 1.0.0 */ public class GetRecordsPagerAdapter extends FragmentPagerAdapter { private final String[] titles = { "本月领取", "累积领取" }; private List<Fragment> fragmentLists; @Override public CharSequence getPageTitle(int position) { // TODO Auto-generated method stub return titles[position]; } public GetRecordsPagerAdapter(FragmentManager fm, List<Fragment> fragmentLists) { super(fm); this.fragmentLists = fragmentLists; } @Override public Fragment getItem(int position) { // TODO Auto-generated method stub return fragmentLists.get(position); } @Override public int getCount() { // TODO Auto-generated method stub return fragmentLists.size(); } }
MonthGetRecordFragment.java和TotalGetRecordFragment的代码如下所示
public class MonthGetRecordFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.month_get_record, container, false); } }