<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.design.widget.TabLayout
android:id="@+id/tab"
android:layout_width="match_parent"
app:tabTextColor="#00ff96"
app:tabSelectedTextColor="#ff0033"
app:tabBackground="@color/colorPrimaryDark"
android:layout_height="0dp"
android:layout_weight="1"></android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"></android.support.v4.view.ViewPager>
</LinearLayout>
------------------------------------------------------------------------------------------------
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<Fragment> fragments;
private List<String> tabs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找控件
TabLayout tab = (TabLayout) findViewById(R.id.tab);
ViewPager vp = (ViewPager) findViewById(R.id.vp);
tabs = new ArrayList<>();
tabs.add("tab0");
tabs.add("tab1");
tabs.add("tab2");
tabs.add("tab3");
//设置 TabLayout模式
tab.setTabMode(TabLayout.MODE_FIXED);
//设置TabLayout标签 添加tab选项卡
tab.addTab(tab.newTab().setText(tabs.get(0)));
tab.addTab(tab.newTab().setText(tabs.get(1)));
tab.addTab(tab.newTab().setText(tabs.get(2)));
tab.addTab(tab.newTab().setText(tabs.get(3)));
fragments = new ArrayList<>();
fragments.add(new Tab0Fragment());
fragments.add(new Tab1Fragment());
fragments.add(new Tab2Fragment());
fragments.add(new Tab3Fragment());
MyFragmentAdapter adapter = new MyFragmentAdapter(getSupportFragmentManager());
vp.setAdapter(adapter);
//将TabLayout与ViewPager关联起来
tab.setupWithViewPager(vp);
}
private class MyFragmentAdapter extends FragmentPagerAdapter {
public MyFragmentAdapter(FragmentManager supportFragmentManager) {
super(supportFragmentManager);
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return tabs.get(position);
}
}
}
------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="这是标签0页面"/>
</android.support.constraint.ConstraintLayout>
------------------------------------------------------------------------------------------------
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* 1. 类的用途
* 2. @author forever
* 3. @date 2018/7/18 11:37
*/
public class Tab0Fragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.tab0, null);
return view;
}
}
------------------------------------------------------------------------------------------------
依赖:
compile 'com.android.support:design:27.1.1'