android BottomNavigationView实现底部导航栏

Android BottomNavigationView 实现
本文详细介绍了如何在Android应用中使用BottomNavigationView组件实现底部导航功能。包括依赖项添加、布局配置、菜单设置、状态选择器定义及Fragment切换的Java代码实现。

 

implementation 'com.android.support:design:28.0.0'

 在layout编写

<android.support.design.widget.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        app:itemTextColor="@color/colorPrimary"
        app:itemIconTint="@drawable/select_button"
        app:menu="@menu/menu_bottom_navigation"
        />
    <!--app:itemIconTint 为图标颜色变化 , app:itemtextColor 为为文字颜色变化
-->

 在res创建menu文件夹——》menu_bottom_navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/item_1"
        android:icon="@color/design_default_color_primary"
        android:title="aa"
        />
    <item
        android:id="@+id/item_2"
        android:icon="@color/design_default_color_primary_dark"
        android:title="bb"
        />

    <item
        android:id="@+id/item_3"
        android:icon="@color/colorAccent"
        android:title="cc"
        />
</menu>

在drawable文件创建-》select_button.xml

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

java代码

Fragment1 fragment1;
    Fragment2 fragment2;
    Fragment3 fragment3;
    Fragment[] fragments;
    int lastfragment=0;
    private void initFragment(){
        fragment1=new Fragment1();
        fragment2=new Fragment2();
        fragment3=new Fragment3();
        fragments=new Fragment[]{fragment1,fragment2,fragment3};

        lastfragment=0;

        getSupportFragmentManager().beginTransaction().replace(R.id.mainview,fragment1).show(fragment1).commit();
        bottomNavigationView=findViewById(R.id.bottomNavigationView);
        bottomNavigationView.setOnNavigationItemSelectedListener(changeFragment);

    }

    private BottomNavigationView.OnNavigationItemSelectedListener changeFragment= new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            Log.d("提示:","1");
            switch (item.getItemId())
            {
                case R.id.item_1:
                {
                    Log.d("提示:","1");
                    if(lastfragment!=0)
                    {
                        switchFragment(lastfragment,0);
                        lastfragment=0;

                    }

                    return true;
                }
                case R.id.item_2:
                {
                    Log.d("提示:","2");
                    if(lastfragment!=1)
                    {
                        switchFragment(lastfragment,1);
                        lastfragment=1;

                    }

                    return true;
                }
                case R.id.item_3:
                {
                    Log.d("提示:","3");
                    if(lastfragment!=2)
                    {
                        switchFragment(lastfragment,2);
                        lastfragment=2;

                    }

                    return true;
                }


            }


            return false;
        }
    };

    private void switchFragment(int lastfragment,int index)
    {
        FragmentTransaction transaction =getSupportFragmentManager().beginTransaction();
        transaction.hide(fragments[lastfragment]);//隐藏上个Fragment
        if(fragments[index].isAdded()==false)
        {
            transaction.add(R.id.mainview,fragments[index]);


        }
        transaction.show(fragments[index]).commitAllowingStateLoss();


    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值