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.

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

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

在drawable文件创建-》select_button.

<?  version=\"1.0\" encoding=\"utf-8\"?>
<selector  ns: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();


    }

 

收藏 打印