Android Material Design 之 BottomNavigationView

概述

BottomNavigationView 是 MD 风格的底部导航视图,也是比较常见的视图之一。
详细可以参照 MD 的官方文档 https://material.io/guidelines/components/bottom-navigation.html#bottom-navigation-style

简单实现

首先添加 design library

compile 'com.android.support:design:25.4.0'

布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:menu="@menu/bottom_navigation" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_above="@id/bottom_navigation"
        android:background="@android:color/darker_gray" />
</RelativeLayout>

bottom_navigation.xml

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

    <item
        android:id="@+id/item_home"
        android:icon="@drawable/ic_home"
        android:title="主页" />

    <item
        android:id="@+id/item_share"
        android:icon="@drawable/ic_share"
        android:title="分享" />

    <item
        android:id="@+id/item_settings"
        android:icon="@drawable/ic_settings"
        android:title="设定" />

</menu>

代码很简单,这里就不再解释了。

效果如下:

高级设置

图标的颜色是可以修改的,做法如下

app:itemIconTint="@color/bottom_navigation_item_color"

添加 itemIconTint 属性来设置选中状态和未选中状态下的图标颜色。

bottom_navigation_item_color.xml 代码如下

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/colorAccent" android:state_checked="true" />
    <item android:color="@android:color/darker_gray" />
</selector>

效果如下:

图标的颜色改掉了,但是文字的颜色没有改掉。

继续添加 itemTextColor 属性即可

app:itemTextColor="@color/bottom_navigation_item_color"

效果如下:

添加点击事件

BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Toast.makeText(MainActivity.this, item.getTitle(), Toast.LENGTH_SHORT).show();
        return true;
    }
});

官方建议 1~2 个 Item 的话不应该使用 BottomNavigationView,并且 Item 的数量不应该超过 5 个,如果你设置了 6 个 Item 的话,会直接报以下错误

Caused by: java.lang.IllegalArgumentException: Maximum number of items supported by BottomNavigationView is 5. Limit can be checked with BottomNavigationView#getMaxItemCount()

我们继续添加 Item 到 5 个,效果变成下面这样了

上图中的效果叫做 ShiftingMode,通过查看源码发现,只要 Item 的数量达到 4 个的时候,就会开启 ShiftingMode。

如果不想要 ShiftingMode,可以利用反射来实现,具体做法参照下面的链接
https://stackoverflow.com/questions/40176244/how-to-disable-bottomnavigationview-shift-mode

源码

https://github.com/teletian/Android/tree/master/MaterialDesignSamples/BottomNavigationView

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值