继承FragmentActivity
清单文件中要修改的部分
android:theme="@style/StyledIndicators"
values下的styles下要添加
<style name="StyledIndicators" parent="@android:style/Theme.Light">
<item name="vpiTabPageIndicatorStyle">@style/CustomTabPageIndicator</item>
</style>
<style name="CustomTabPageIndicator" parent="Widget.TabPageIndicator">
<item name="android:background">@drawable/tab_indicator</item>
<item name="android:textAppearance">@style/CustomTabPageIndicator.Text</item>
<item name="android:textSize">14sp</item>
<item name="android:dividerPadding">8dp</item>
<item name="android:showDividers">middle</item>
<item name="android:paddingLeft">10dp</item>
<item name="android:paddingRight">10dp</item>
<item name="android:fadingEdge">horizontal</item>
<item name="android:fadingEdgeLength">8dp</item>
</style>
<style name="CustomTabPageIndicator.Text" parent="android:TextAppearance.Medium">
<item name="android:typeface">monospace</item>
<item name="android:textColor">@drawable/selector_tabtext</item>
</style>
资源文件中drawable的文件
1.selector_tabtext.xml
2.tab_indicator.xml
添加图片
base_tabpager_indicator_selected.9.png
代码部分
//获得控件
indicator=(TabPageIndicator)findViewById(R.id.indicator);
vp=(ViewPager)findViewById(R.id.vp);
//给fragment传值
addfragment();
//添加适配器
ad=new adapter(getSupportFragmentManager(), list, str);
vp.setAdapter(ad);
//绑定viewpager
indicator.setViewPager(vp);
//addfragment里
list = new ArrayList<F1>();
for(int i=0;i<str.length;i++){
//fragment
F1 f=new F1();
Bundle bundle=new Bundle();
bundle.putString("path", str[i]+"+++");
f.setArguments(bundle);
//自定义集合泛型为fragment
list.add(f);
}
//布局
<com.viewpagerindicator.TabPageIndicator
android:id="@+id/indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></android.support.v4.view.ViewPager>
//适配器部分
List<F1> list;
String[] str;
public adapter(FragmentManager fm, List<F1> list, String[] str) {
super(fm);
this.list = list;
this.str = str;
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public CharSequence getPageTitle(int position) {
// TODO Auto-generated method stub
return str[position];
}