BottomNavigationView 使用

添加依赖:

    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support:design:26.+'

注意: support:design的版本要和support:appcompat的版本一致。否则编译不通过。

代码中使用

layout布局:
<android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/life_red"
        app:itemIconTint="@color/life_white"
        app:itemTextColor="@color/life_white"
        app:menu="@menu/menu_material_bottom_navigation"
        android:layout_gravity="bottom">

    </android.support.design.widget.BottomNavigationView>
menu布局
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:icon="@drawable/home_page_black"
        android:title="@string/home_page" />
    <item
        android:icon="@drawable/icons8_compass_96"
        android:title="@string/find" />
    <item
        android:icon="@drawable/icons8_product_96"
        android:title="@string/product" />
    <item
        android:icon="@drawable/icons8_user_96"
        android:title="@string/me" />
</menu>
默认效果
out.gif

为图标和字体换颜色

        app:itemIconTint="@color/life_white"
        app:itemTextColor="@color/life_white"

只能设置color不能设置drawable

  1. 在res目录下创建color资源文件夹
  2. 创建selector文件material_bottom_navigation
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/life_white" android:state_pressed="true"/>
    <item android:color="@color/life_white" android:state_checked="true"/>
    <item android:color="@color/life_black"/>
</selector
  1. 替换
        app:itemIconTint="@color/material_bottom_navigation"
        app:itemTextColor="@color/material_bottom_navigation"

问题

  • 当item = 3个时,没有动画,大于3个 会出现上面的动画效果。
  • 扩展性差

推荐文章:
https://juejin.im/post/59f924b0f265da431f4a60c3
https://www.jianshu.com/p/aa499cc64f72
https://material.io/develop/android/components/bottom-navigation-view/

目前市面上有比较不错的三方:
https://github.com/aurelhubert/ahbottomnavigation
https://github.com/chaychan/BottomBarLayout

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,812评论 2 45
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    wgl0419阅读 6,547评论 1 9
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 177,534评论 25 709
  • 这篇文章主要解决两个问题:1.自定义导航栏返回按钮右滑返回手势失效的问题。2.实现整个屏幕右滑就返回功能。(动画效...
    PandaXiong阅读 3,944评论 1 5
  • lavieena阅读 142评论 0 0

友情链接更多精彩内容