效果圖示例:java
一、res/layout下只有一個activity_main.xml佈局android
activity_main.xml佈局ide
代碼佈局
<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="${relativePackage}.${activityClass}" >字體
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content" >this
<android.support.v4.view.PagerTabStrip
android:id="@+id/pagerTabStrip"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.v4.view.ViewPager>spa
</RelativeLayout>xml
=============================ip
二、源文件有2個一個MainActivity.java一個PagerFragment.javaci
PagerFragment.java類
代碼
public class PagerFragment extends Fragment {
private int tabIndex;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
tabIndex = getArguments().getInt("tabIndex");
}
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
TextView text = new TextView(getActivity());
switch(tabIndex){
case 0:
text.setText("記錄");
break;
case 1:
text.setText("聯繫人");
break;
case 2:
text.setText("收藏夾");
break;
case 3:
text.setText("羣組");
break;
}
return text;
}
}
=====================
三、MainActivity類
代碼
public class MainActivity extends FragmentActivity {
private ViewPager viewPager;
private PagerTabStrip pagerTabStrip;
private List<Fragment> list;
private String[] titels = {"記錄","聯繫人","收藏夾","羣組"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.viewPager = (ViewPager) this.findViewById(R.id.viewPager);
this.pagerTabStrip = (PagerTabStrip) this.findViewById(R.id.pagerTabStrip);
//設置PagerTabStrip樣式
pagerTabStrip.setTextColor(Color.WHITE);//字體顏色
pagerTabStrip.setBackgroundColor(Color.GRAY);//背景顏色
pagerTabStrip.setDrawFullUnderline(true);//下劃線
pagerTabStrip.setTabIndicatorColor(Color.CYAN);//下劃線顏色
pagerTabStrip.setTextSpacing(30);//每一個tab間隔
list = new ArrayList<Fragment>();
for(int i = 0;i<titels.length;i++){
PagerFragment fragment = new PagerFragment();
Bundle bundle = new Bundle();
bundle.putInt("tabIndex", i);
fragment.setArguments(bundle);
list.add(fragment);
}
viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(),list));
}
class MyFragmentPagerAdapter extends FragmentPagerAdapter{
private List<Fragment> list;
public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> list) {
super(fm);
// TODO Auto-generated constructor stub
this.list = list;
}
@Override
public Fragment getItem(int position) {
// TODO Auto-generated method stub
return this.list.get(position);
}
@Override public int getCount() { // TODO Auto-generated method stub return this.list.size(); } @Override//設置tab標題 而且和ViewPager關聯 public CharSequence getPageTitle(int position) { // TODO Auto-generated method stub return titels[position]; } }}