美文网首页Android那点事程序员
Android底部导航栏之官方BottomNavigationV

Android底部导航栏之官方BottomNavigationV

作者: NickelFox | 来源:发表于2017-04-13 22:19 被阅读421次

今天将AS升级2.3.1之后新建工程,忽然发现竟然多了这个!!!看来不得不去了解了,毕竟是官方控件

Bottom Navigation Activity

一. 简介

  1. 官方简介

Represents a standard bottom navigation bar for application. It is an implementation of material design bottom navigation.
应用底部的导航栏,是material design bottom navigation的实现.

  1. 特性,使用场景

Bottom navigation bars make it easy for users to explore and switch between top-level views in a single tap. It should be used when application has three to five top-level destinations.
底部导航使用户更方便的查看和切换最高层级的导航界面,适用于有三到五个Tab的情况(推荐是三到五个,太多和太少都不应该用)

二. 用法

  1. build.gradle加入支持库
compile 'com.android.support:design:25.2.0'
  1. XML使用布局(只是NavigationView,具体Tab要在menu里添加)
<android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/navigation"/>
  1. 添加Tab
    3.1 官方介绍

The bar contents can be populated by specifying a menu resource file. Each menu item title, icon and enabled state will be used for displaying bottom navigation bar items. Menu items can also be used for programmatically selecting which destination is currently active. It can be done using MenuItem#setChecked(true)
可以通过menu的方式向BottomNavigationView添加Tab,每个menu可以有title,icon,enable状态,可以通过menu的checked属性设置默认选中的Tab(我自己设置无效)

3.2 添加Menu

<menu xmlns:android="http://schemas.android.com/apk/res/android">

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

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

    <!-- 该item设置enabled为false,界面会显示当前item但是是灰色,不能点击-->
    <item
        android:id="@+id/navigation_notifications"
        android:icon="@drawable/ic_notifications_black_24dp"
        android:title="@string/title_notifications"
        android:enabled="false"/>
</menu>

3.3 设置点击事件

        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.getMenu().getItem(2).setChecked(true);//设置默认选中的Tab
        navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                /*通过判断进行选择Tab之后想要的操作,一般是进行fragment的动态切换*/
                switch (item.getItemId()) {
                    case R.id.navigation_home:
                        mTextMessage.setText(R.string.title_home);
                        return true;
                    case R.id.navigation_dashboard:
                        mTextMessage.setText(R.string.title_dashboard);
                        return true;
                    case R.id.navigation_notifications:
                        mTextMessage.setText(R.string.title_notifications);
                        return true;
                }
                return false;
            }
        });

三. XML属性介绍

Caution:以下三种属性都可以用ColorStateList来实现点击前后颜色变化效果

  • itemBackground 设置NavigationView的背景颜色,效果如下图:
itemBackground效果图 itemIconTint效果 itemTextColor效果
PS:以上三个属性不设置的话默认背景颜色为background,图标和字体颜色为灰色

四. Tab点击后的颜色设置

  1. 新建ColorStateList(colot_item_state.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="@color/colorAccent"/>
    <item
        android:color="@color/colorPrimary"/>
</selector>
该文件要在res文件夹下新建color目录,然后新建该xml文件
  1. 通过NavigationView的三个XML属性设置该ColorStateList
<android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/navigation"
        app:itemIconTint="@color/colot_item_state"
        app:itemTextColor="@color/colot_item_state"/>

效果:

点击效果

五. 不同Tab数量的效果图

  1. 三个Tab:


    三个Tab的点击效果
  2. 四个及以上Tab


    四个Tab

相关文章

  • Android底部导航栏之官方BottomNavigationV

    今天将AS升级2.3.1之后新建工程,忽然发现竟然多了这个!!!看来不得不去了解了,毕竟是官方控件 一. 简介 官...

  • 安卓底部导航

    Android底部导航栏实现(一)之BottomNavigationBarAndroid底部导航栏实现(二)之Ra...

  • Android底部导航栏之RadioButton

    参考:五种方式实现Android底部导航栏Android底部导航栏实现(二)之RadioGroup 一. 简介 1...

  • Android底部导航栏之BottomNavigationBar

    参考:五种方式实现Android底部导航栏BottomNavigation(官方)BottomNavigation...

  • BottomNavigationView的属性设置

    底部导航栏 底部导航栏的使用比较常见,目前常用的APP几乎都是使用底部导航栏将内容分类。底部导航栏的实现也比较简单...

  • BottomNavigationBar使用指北

    一.简介当下Android项目中,十之八九都采用底部导航栏的样式做导航菜单交互,Google Material D...

  • 2018-10-30

    Android 使用BottomNavigationView实现底部导航栏 今天我们来使用BottomNaviga...

  • Github之安卓库总结

    Android底部导航栏████几行代码实现Tab导航(随意定制加号,带红点消息提示) 引导界面滑动导航 + 大于...

  • RecycleView 中android:clipToPaddi

    如果想让RecyclerView在透明的导航栏底部滚动,使用android:fitsSystemWindows="...

  • android底部导航栏

    实现跳转到TabsPage的某一个特定的界面,比如最上面的效果图,当从LoginPage跳转到TabsPage的时...

网友评论

    本文标题:Android底部导航栏之官方BottomNavigationV

    本文链接:https://www.haomeiwen.com/subject/udyiattx.html