选项卡

布局:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
 4     android:layout_width="match_parent" android:layout_height="match_parent"
 5     tools:context="com.example.myshishangyuan.MainActivity">
 6 
 7 <android.support.v4.view.ViewPager
 8     android:id="@+id/vp"
 9     android:layout_width="match_parent"
10     android:layout_height="match_parent"
11     android:layout_above="@+id/rg">
12 
13 </android.support.v4.view.ViewPager>
14     <RadioGroup
15         android:id="@+id/rg"
16         android:layout_width="match_parent"
17         android:layout_height="wrap_content"
18         android:orientation="horizontal"
19         android:gravity="center"
20         android:layout_alignParentBottom="true">
21         <RadioButton
22             android:drawableTop="@drawable/yi"
23             android:layout_width="match_parent"
24             android:layout_height="wrap_content"
25             android:layout_weight="1"
26             android:gravity="center"
27             android:button="@null"
28             android:text="新品"
29             android:textSize="15dp"/>
30         <RadioButton
31             android:drawableTop="@drawable/er"
32             android:layout_width="match_parent"
33             android:layout_height="wrap_content"
34             android:layout_weight="1"
35             android:gravity="center"
36             android:button="@null"
37             android:text="精选"
38             android:textSize="15dp"/>
39         <RadioButton
40             android:drawableTop="@drawable/san"
41             android:layout_width="match_parent"
42             android:layout_height="wrap_content"
43             android:layout_weight="1"
44             android:gravity="center"
45             android:button="@null"
46             android:text="分类"
47             android:textSize="15dp"/>
48         <RadioButton
49             android:drawableTop="@drawable/si"
50             android:layout_width="match_parent"
51             android:layout_height="wrap_content"
52             android:layout_weight="1"
53             android:gravity="center"
54             android:button="@null"
55             android:text="我的衣柜"
56             android:textSize="15dp"/>
57         <RadioButton
58             android:drawableTop="@drawable/wu"
59             android:layout_width="match_parent"
60             android:layout_height="wrap_content"
61             android:layout_weight="1"
62             android:gravity="center"
63             android:button="@null"
64             android:text="个人中心"
65             android:textSize="15dp"/>
66 
67     </RadioGroup>
68 
69 
70 </RelativeLayout>

Activity:

 1 public class MainActivity extends AppCompatActivity {
 2     private FragmentManager fm;
 3     private ArrayList<Fragment> list;
 4     private ViewPager vp;
 5     private RadioGroup rg;
 6     private RadioButton[] rbt;
 7     private MyFragment myFragment;
 8 
 9 
10     @Override
11     protected void onCreate(Bundle savedInstanceState) {
12         super.onCreate(savedInstanceState);
13         setContentView(R.layout.activity_main);
14         vp = (ViewPager) findViewById(R.id.vp);
15         rg = (RadioGroup) findViewById(R.id.rg);
16         rbt=new RadioButton[rg.getChildCount()];
17         
18         for (int i = 0; i <rbt.length ; i++) {
19             rbt[i]= (RadioButton) rg.getChildAt(i);
20         }
21         rbt[0].setChecked(true);
22 
23 
24          list = new ArrayList<>();
25         Fragment_xp fragment_xp = new Fragment_xp();
26         Fragment_jx fragment_jx = new Fragment_jx();
27         Fragment_fl fragment_fl = new Fragment_fl();
28         Fragment_wdyg fragment_wdyg = new Fragment_wdyg();
29         Fragment_grzx fragment_grzx = new Fragment_grzx();
30 
31         list.add(fragment_xp);
32         list.add(fragment_jx);
33         list.add(fragment_fl);
34         list.add(fragment_wdyg);
35         list.add(fragment_grzx);
36          fm = getSupportFragmentManager();
37         myFragment = new MyFragment(fm, list);
38         vp.setAdapter(myFragment);
39         vp.setCurrentItem(0);
40         rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
41             @Override
42             public void onCheckedChanged(RadioGroup radioGroup, int i) {
43                 for (int j = 0; j <rbt.length ; j++) {
44                     if (rbt[j].getId()==i){
45                         vp.setCurrentItem(j);
46                     }
47                 }
48             }
49         });
50 
51     }
52 
53 }
原文地址:https://www.cnblogs.com/SongYongQian/p/7883196.html