Android 导航 BottomNavgiationView

2020-10-12  本文已影响0人  嗯哼_e683

Android的底部导航栏 效果如下:


底部导航效果.png

代码如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize">

   <!--  底部导航栏  -->
    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_btn_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu"
        android:layout_alignParentBottom="false"
        android:elevation="1dp"
        app:elevation="1dp"
        app:itemBackground="@null"
        app:itemHorizontalTranslationEnabled="false"
        app:labelVisibilityMode="labeled"/>

    <!--  navigation导航栏  -->
    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_btn_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

常用属性

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/navigationbtm_home"
        android:icon="@drawable/select_bottom_bar_home"
        android:title="@string/title_home" />

    <item
        android:id="@+id/navigationbtm_find"
        android:icon="@drawable/select_bottom_bar_find"
        android:title="@string/title_dashboard" />

    <item
        android:id="@+id/navigationbtm_my"
        android:icon="@drawable/ic_notifications_black_24dp"
        android:title="@string/title_notifications" />

</menu>

icon图片使用的选择器

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

点击事件的监听:

navBtnView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {  //navBtnView控件名
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                NavController navController = Navigation.findNavController(MainActivity.this, R.id.nav_host_fragment);
                switch (item.getItemId()){
                    case R.id.navigationbtm_home:
                        navController.navigate(R.id.navigation_home);
                        showToast("主页");
                        break;
                    case R.id.navigationbtm_find:
                        showToast("发现");
                        navController.navigate(R.id.navigation_find);
                        break;
                    case R.id.navigationbtm_my:
                        showToast("我的");
                        navController.navigate(R.id.navigation_my);
                        break;
                }
                return true;
            }
        });
上一篇 下一篇

猜你喜欢

热点阅读