先上效果图:

许多应用都会带有这种底部选项卡,应用非常广泛,所以现总结一套模式,方便以后使用:
其设计模式非常简单:RadioGroup+RadioButton+FrameLayout+Fragment
1.MainActivity的布局:

<LinearLayout 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"    android:orientation="vertical"    tools:context=".MainActivity" >    <FrameLayout        android:id="@+id/fl"        android:layout_width="fill_parent"        android:layout_height="0dp"        android:layout_weight="1" >    </FrameLayout> <RadioGroup android:id="@+id/rg" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/bottom_tab_bg" android:gravity="center_vertical" android:orientation="horizontal" > <RadioButton android:id="@+id/rb1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:drawableTop="@drawable/selector_home" android:gravity="center" android:text="选项1" android:textColor="@color/selector_color_text" /> <RadioButton android:id="@+id/rb2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:drawableTop="@drawable/selector_center" android:gravity="center" android:text="选项2" android:textColor="@color/selector_color_text" /> <RadioButton android:id="@+id/rb3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:drawableTop="@drawable/selector_smartservice" android:gravity="center" android:text="选项3" android:textColor="@color/selector_color_text" /> <RadioButton android:id="@+id/rb4" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:drawableTop="@drawable/selector_gov" android:gravity="center" android:text="选项4" android:textColor="@color/selector_color_text" /> <RadioButton android:id="@+id/rb5" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:drawableTop="@drawable/selector_setting" android:gravity="center" android:text="选项5" android:textColor="@color/selector_color_text" /> </RadioGroup> </LinearLayout>

注意:RadioButton 中有一个属性:android:button=”@null” 可以去掉RadioButton上的一个小圈圈

2.MainActivity中的代码:

public class MainActivity extends FragmentActivity {    private ArrayList<Fragment> fragmentsList = new ArrayList<Fragment>();    private RadioGroup group;    @Override    protected void onCreate(Bundle arg0) {        super.onCreate(arg0);        setContentView(R.layout.activity_main);        group = (RadioGroup) findViewById(R.id.rg);        // 给group设置监听事件,在监听事件实现fragment之间的切换        OnCheckedChangeListener listener = new MyOnCheckedChangeListener();        group.setOnCheckedChangeListener(listener);        // 选中首页,否则开始启动的时候画面展示白板        group.check(R.id.rb1);    }    private class MyOnCheckedChangeListener implements OnCheckedChangeListener {        // 在构造方法中创造fragment        public MyOnCheckedChangeListener() {            // 将new出来的fragment放置在集合中,以便后续取用            fragmentsList.add(new Fragment1());            fragmentsList.add(new Fragment2());            fragmentsList.add(new Fragment3());            fragmentsList.add(new Fragment4());            fragmentsList.add(new Fragment5());        }        @Override        public void onCheckedChanged(RadioGroup group, int checkedId) {            // 当选中某一个radio的时候,就展现某一个fragment,用到fragment的事务            FragmentTransaction ft = getSupportFragmentManager()                    .beginTransaction();            switch (checkedId) {            case R.id.rb1:                ft.replace(R.id.fl, fragmentsList.get(0));                break;            case R.id.rb2:                ft.replace(R.id.fl, fragmentsList.get(1));                break;            case R.id.rb3:                ft.replace(R.id.fl, fragmentsList.get(2));                break;            case R.id.rb4:                ft.replace(R.id.fl, fragmentsList.get(3));                break;            case R.id.rb5:                ft.replace(R.id.fl, fragmentsList.get(4));                break;            default:                break;            }            // 最后事务一定要提交            ft.commit();        }    }}

更多相关文章

  1. Android部分功能模块实现
  2. ym——Android之常用Tools【介绍及使用】
  3. android 检测外接键盘并设置输入法布局
  4. android RadioGroup实现单选以及默认选中
  5. Android中Button的几种点击的写法
  6. android radioButton 改变圆圈大小
  7. android监听返回按钮事件
  8. SwipeRecyclerview使用中一些常见错误处理方法【持续更新中】
  9. android开发者选项

随机推荐

  1. Android基于ProductFlavors动态配置权限
  2. CyanogenMod源码下载和编译(Android ROM
  3. Eclipse(Windowns XP)下搭建Android开发
  4. 怎样查看程序的历史运行时间?
  5. Android 新版个税计算小demo程序
  6. Android中Build Target,minSdkVersitarge
  7. view对touch事件的处理
  8. android post 提交数据
  9. [转]android 几何图形的绘制
  10. 【Android Studio】Android Studio出现资