使用RadioGroup实现底部导航栏

本文介绍如何利用RadioGroup在Android中实现底部导航栏功能,通过内嵌Fragment和使用Selector实现点击切换Fragment并改变图标和文字颜色。同时,文中还提及了双击返回键退出应用的设置。

效果如图:

实现可最基本的导航栏功能,不能左右滑动,只能点击

1.内嵌的fragment的布局:

<?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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textSize="50sp"
        android:textColor="@color/colorPrimary"
        android:text="home"/>
</LinearLayout>

2.fragment的activity代码:

public class FrHome extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.fragment_home, container, false);
        return view;
    }

}

以此为例根据需要编写不同的fragment布局等等。

3.装载fragment的界面布局如下(其中使用了selector进行实现点击改变图标和文字颜色):

点击改变文字颜色:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="#3F51B5"/>
    <item android:state_checked="false" android:color="#8f8f8f"/>
</selector>

点击改变图标:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@mipmap/ic_history_checked"/>
    <item android:state_checked="false" android:drawable="@mipmap/ic_history_unchecked"/>
</selector>

界面布局:

<?xml version="1.0" encoding="utf-8"?>
<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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.lotus.chartspagedemo.ActHome">

    <FrameLayout
        android:id="@+id/frame_layout"
        android:layout_width="match_parent"
        android:layout_above="@+id/card_view"
        android:layout_height="match_parent"/>

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        app:cardElevation="25dp"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RadioGroup
            android:paddingTop="5dp"
            android:id="@+id/tab_bar"
            android:background="@color/app_white"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:gravity="center"
            android:orientation="horizontal">
            <RadioButton
                android:id="@+id/tab_home"
                android:gravity="center"
                android:button="@null"
                android:drawableTop="@drawable/selector_tab_home"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:textColor="@drawable/selector_tab_color"
                android:text="首页"/>
            <RadioButton
                android:id="@+id/tab_health"
                android:gravity="center"
                android:button="@null"
                android:drawableTop="@drawable/selector_tab_health"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:textColor="@drawable/selector_tab_color"
                android:text="体检测评" />
            <RadioButton
                android:id="@+id/tab_personal"
                android:gravity="center"
                android:button="@null"
                android:drawableTop="@drawable/selector_tab_personal"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:textColor="@drawable/selector_tab_color"
                android:text="个人中心"
                />
        </RadioGroup>
    </android.support.v7.widget.CardView>

</RelativeLayout>

4.装载fragment的界面的activity代码(加入双击返回键则退出应用):

public class ActHome extends FragmentActivity implements RadioGroup.OnCheckedChangeListener {

    @BindView(R.id.frame_layout)
    FrameLayout frameLayout;
    @BindView(R.id.tab_home)
    RadioButton tabHome;
    @BindView(R.id.tab_health)
    RadioButton tabHealth;
    @BindView(R.id.tab_personal)
    RadioButton tabPersonal;
    @BindView(R.id.tab_bar)
    RadioGroup tabBar;

    public final static String ACTION_EXIT_SYSTEM = "sys_exit";

    private FragmentManager manager;
    private FragmentTransaction transaction;
    private FrHome frHome;
    private FrHealth frHealth;
    private FrPersonal frPersonal;
    private long mExitTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);
        RadioButton tabHome = (RadioButton) tabBar.getChildAt(0);
        tabHome.setChecked(true);
        tabBar.setOnCheckedChangeListener(this);
        initFragment();
    }

    private void initFragment() {
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        frHome = new FrHome();
        transaction.add(R.id.frame_layout,frHome);
        transaction.commit();
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int checkedId) {
        switch (checkedId) {
            case R.id.tab_home:
                FragmentTransaction ft1 = manager.beginTransaction();
                hideAll(ft1);
                if (frHome!=null){
                    ft1.show(frHome);
                }else {
                    frHome=new FrHome();
                    ft1.add(R.id.frame_layout,frHome);
                }
                ft1.commit();
                break;
            case R.id.tab_health:
                FragmentTransaction ft2 = manager.beginTransaction();
                hideAll(ft2);
                if (frHealth!=null){
                    ft2.show(frHealth);
                }else {
                    frHealth = new FrHealth();
                    ft2.add(R.id.frame_layout,frHealth);
                }
                ft2.commit();
                break;
            case R.id.tab_personal:
                FragmentTransaction ft5 = manager.beginTransaction();
                hideAll(ft5);
                if (frPersonal!=null){
                    ft5.show(frPersonal);
                }else {
                    frPersonal = new FrPersonal();
                    ft5.add(R.id.frame_layout, frPersonal);
                }
                ft5.commit();
                break;
        }
    }

    private void hideAll(FragmentTransaction ft){
        if (ft==null){
            return;
        }
        if (frHome!=null){
            ft.hide(frHome);
        }
        if (frHealth!=null){
            ft.hide(frHealth);
        }
        if (frPersonal!=null){
            ft.hide(frPersonal);
        }
    }

    @Override
    public void onBackPressed() {
        if ((System.currentTimeMillis() - mExitTime) > 2000) {
            Toast.makeText(ActHome.this,"再按一次退出程序",Toast.LENGTH_SHORT).show();
            mExitTime = System.currentTimeMillis();
        } else {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    onExit(ActHome.this);
                }
            }, 500);
        }
    }

    public static void onExit(final Context context) {
        try {
            Intent intent = new Intent();
            intent.setAction(context.getApplicationContext().getPackageName() + ACTION_EXIT_SYSTEM);
            context.sendBroadcast(intent);
            // MobclickAgent.onKillProcess(context);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    System.exit(0);
                }
            }, 200);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值