当前位置: 首页 > 工具软件 > Bottom > 使用案例 >

使用 BottomNavigationView实现android应用的底部导航(support库)

燕琛
2023-12-01

1.简介

BottomNavigationView是一个底部导航栏控件,一般和fragment一起使用。



2.使用

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottomNavigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:itemTextColor="@drawable/main_bottom_navigation"
        app:itemIconTint="@drawable/main_bottom_navigation"
        app:menu="@menu/navigation">
    </android.support.design.widget.BottomNavigationView>

点击android.support.design.widget.BottomNavigationView 导入

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

控件分析:
app:itemTextColor 指的是导航栏文字的颜色
app:itemIconTint 指的是导航栏中图片的颜色
app:iteamBackground 指的是底部导航栏的背景颜色,默认是主题的颜色
app:menu 指的是布局(文字和图片都写在这个里面)

注意:关于app:的使用,都需要在最外层里面加入下面这个
xmlns:app="http://schemas.android.com/apk/res-auto" 系统会默认导入的,如果没有就手动导入。



3.navigation界面

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/main_wallpaper"
        android:icon="@drawable/ic_wallpaper"
        android:title="@string/main_wallpaper" />
    <item
        android:id="@+id/main_music"
        android:icon="@drawable/ic_music"
        android:title="@string/main_music" />
    <item
        android:id="@+id/main_my"
        android:icon="@drawable/ic_user"
        android:title="@string/main_user" />
</menu>

ic_开头的是图片
main_开头的是文字

可以从icon和title来看出来,这里我建议大家使用文字的时候尽量不要写在xml里面,而是写在strings里面,因为我经常做多国语言的翻译,有这个习惯。这个习惯会让你以后对文字的优化,或者添加都有好的提升。

下面我们看控件点击时候的颜色



4.main_bottom_navigation的界面

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

item里面有很多的属性,例如focus,drawable。



5.完整的activity代码

import android.os.Build;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;

import bottomnavigationview.luo.com.bottomnavigationview02.fragment.MusicFragment;
import bottomnavigationview.luo.com.bottomnavigationview02.fragment.MyFragment;
import bottomnavigationview.luo.com.bottomnavigationview02.fragment.WallpaperFragment;

/**
 * Created by luo on 2019/7/3.
 */

public class MainActivity extends AppCompatActivity {

    private FrameLayout mainFrame;
    private BottomNavigationView bottomNavigation;
    private WallpaperFragment wallpaperFragment;
    private MusicFragment musicFragment;
    private MyFragment myFragment;
    private Fragment[] fragments;
    private int lastfragment = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setHalfTransparent();
        initView();
    }

    private void initView() {
        wallpaperFragment = new WallpaperFragment();
        musicFragment = new MusicFragment();
        myFragment = new MyFragment();
        fragments = new Fragment[]{wallpaperFragment, musicFragment, myFragment};
        mainFrame = (FrameLayout) findViewById(R.id.mainFrame);
        //设置fragment到布局
        getSupportFragmentManager().beginTransaction().replace(R.id.mainFrame, wallpaperFragment).show(wallpaperFragment).commit();

        bottomNavigation = (BottomNavigationView) findViewById(R.id.bottomNavigation);

        //这里是bottomnavigationview的点击事件  
        bottomNavigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

    }

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.main_wallpaper:
                  //这里因为需要对3个fragment进行切换
                  //start 
                    if (lastfragment != 0) {
                        switchFragment(lastfragment, 0);
                        lastfragment = 0;
                    }
                  //end    
                  //如果只是想测试按钮点击,不管fragment的切换,可以把start到end里面的内容去掉
                    return true;
                case R.id.main_music:
                    if (lastfragment != 1) {
                        switchFragment(lastfragment, 1);
                        lastfragment = 1;
                    }
                    return true;
                case R.id.main_my:
                    if (lastfragment != 2) {
                        switchFragment(lastfragment, 2);
                        lastfragment = 2;
                    }
                    return true;
                default:
                    break;
            }
            return false;
        }
    };

    /**
    *切换fragment
    */
    private void switchFragment(int lastfragment, int index) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        //隐藏上个Fragment
        transaction.hide(fragments[lastfragment]);
        if (fragments[index].isAdded() == false) {
            transaction.add(R.id.mainFrame, fragments[index]);
        }
        transaction.show(fragments[index]).commitAllowingStateLoss();
    }

    /**
     * 半透明状态栏
     */
    protected void setHalfTransparent() {
        if (Build.VERSION.SDK_INT >= 21) {//21表示5.0
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        } else if (Build.VERSION.SDK_INT >= 19) {//19表示4.4
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //虚拟键盘也透明
            // getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }
}



6.小结

1.BottomNavigationView需要导包,与appcompat一致
2.需要一个menu的xml,文字和图片
3.按钮的点击设置



7.android studio自动创建BottomNavigationView

案例demo 下载:

链接:https://pan.baidu.com/s/1uqFj-Hjgv5QnVBRAQApLcw 
提取码:b397

 类似资料: