首先来张效果图
1、MainActivity.java
public class MainActivity extends ActionBarActivity { private static final int RB_NEW = 0; private static final int RB_UPLOAD = 1; private static final int RB_FRESH = 2; private static final int RB_SORT = 3; private static final int RB_TRANSFER = 4; private RadioButton[] rbs = new RadioButton[5]; private ViewPager viewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rbs[0] = (RadioButton) this.findViewById(R.id.rbNew); rbs[1] = (RadioButton) this.findViewById(R.id.rbUpload); rbs[2] = (RadioButton) this.findViewById(R.id.rbFresh); rbs[3] = (RadioButton) this.findViewById(R.id.rbSort); rbs[4] = (RadioButton) this.findViewById(R.id.rbTransfer); for(int i=0; i<rbs.length; i++) { rbs[i].setOnClickListener(new RBOnClickListener()); } FragmentAdapter fragmentAdapter = new FragmentAdapter(getSupportFragmentManager()); viewPager = (ViewPager) this.findViewById(R.id.viewPager); viewPager.setAdapter(fragmentAdapter); viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int i, float v, int i2) { } @Override public void onPageSelected(int i) { rbs[i].setChecked(true); } @Override public void onPageScrollStateChanged(int i) { } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } class RBOnClickListener implements View.OnClickListener { @Override public void onClick(View v) { switch (v.getId()) { case R.id.rbNew: viewPager.setCurrentItem(RB_NEW); break; case R.id.rbUpload: viewPager.setCurrentItem(RB_UPLOAD); break; case R.id.rbFresh: viewPager.setCurrentItem(RB_FRESH); break; case R.id.rbSort: viewPager.setCurrentItem(RB_SORT); break; case R.id.rbTransfer: viewPager.setCurrentItem(RB_TRANSFER); break; } } } class FragmentAdapter extends FragmentPagerAdapter { FragmentAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int i) { switch (i) { case RB_NEW: Fragment fragment1 = new NewFragment(i); return fragment1; case RB_UPLOAD: Fragment fragment2 = new NewFragment(i); return fragment2; case RB_FRESH: Fragment fragment3 = new NewFragment(i); return fragment3; case RB_SORT: Fragment fragment4 = new NewFragment(i); return fragment4; case RB_TRANSFER: Fragment fragment5 = new NewFragment(i); return fragment5; } return null; } @Override public int getCount() { return 5; } } }
2、NewFragment.java
public class NewFragment extends Fragment { private int index; public NewFragment(){ super(); } public NewFragment(int i) { super(); this.index = i; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_new, container, false); TextView textView = (TextView) view.findViewById(R.id.content); textView.setText("Fragment(" + (index + 1) + ")"); return view; } }
3、activity_main.xml
<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=".MainActivity"> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignParentTop="true"/> <RadioGroup android:id="@+id/bottomMenu" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="horizontal" android:background="#666666"> <RadioButton android:id="@+id/rbNew" android:layout_width="0dp" android:layout_height="wrap_content" android:checked="true" android:drawableTop="@drawable/ic_library_add_white_24dp" style="@style/BottomMenu" android:text="新建"/> <RadioButton android:id="@+id/rbUpload" android:layout_width="0dp" android:layout_height="wrap_content" android:drawableTop="@drawable/ic_cloud_upload_white_24dp" style="@style/BottomMenu" android:text="上传"/> <RadioButton android:id="@+id/rbFresh" android:layout_width="0dp" android:layout_height="wrap_content" android:drawableTop="@drawable/ic_autorenew_white_24dp" style="@style/BottomMenu" android:text="刷新"/> <RadioButton android:id="@+id/rbSort" android:layout_width="0dp" android:layout_height="wrap_content" android:drawableTop="@drawable/ic_insert_chart_white_24dp" style="@style/BottomMenu" android:text="排序"/> <RadioButton android:id="@+id/rbTransfer" android:layout_width="0dp" android:layout_height="wrap_content" android:drawableTop="@drawable/ic_swap_vertical_circle_white_24dp" style="@style/BottomMenu" android:text="传输管理"/> </RadioGroup> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal|center_vertical" android:text="新建"/> </LinearLayout>
5、RadionButton的样式
<style name="BottomMenu" parent="Base.TextAppearance.AppCompat.Button"> <item name="android:button">@null</item> <item name="android:textColor">#FFFFFF</item> <item name="android:gravity">center_horizontal</item> <item name="android:layout_weight">1</item> <item name="android:padding">5dp</item> <item name="android:textSize">12sp</item> <item name="android:background">@drawable/bottom_menu_bg</item> </style>