1,在gradle里面添加
compile 'com.android.support:design:26+'
2,创建一个类继承Fragment,重写onCreateView方法
TextView textView=new TextView(getActivity()); double random=Math.random(); textView.setText("xxxxxxxx"+random); return textView;3,主界面里面
private TabLayout tabLayout; private ViewPager viewPager; ArrayList<Fragment> fragmentlist=new ArrayList<Fragment>(); ArrayList<String> titlelist=new ArrayList<String>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tabLayout = (TabLayout) findViewById(R.id.tabLayout); viewPager = (ViewPager) findViewById(R.id.viewPager); InitData(); MPagerAdapter mPagerAdapter=new MPagerAdapter(getSupportFragmentManager()); viewPager.setAdapter(mPagerAdapter); //设置TabLayout的模式 tabLayout.setTabMode(TabLayout.MODE_FIXED); //让tablayout和viewpager关联 tabLayout.setupWithViewPager(viewPager); } private void InitData() { for (int i=0;i<4;i++){ fragmentlist.add(new BlankFragment()); titlelist.add("哈哈哈"+i); } } class MPagerAdapter extends FragmentPagerAdapter{ public MPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return fragmentlist.get(position); } @Override public int getCount() { return fragmentlist.size(); } @Override public CharSequence getPageTitle(int position) { return titlelist.get(position); } }