前言

  注意这个里介绍的是AndroidX的com.google.android.material.bottomnavigation.BottomNavigationView

xml布局中

   <com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:menu="@menu/p_home_bottom_menu"
app:labelVisibilityMode="labeled"
app:itemTextColor="@color/fontBlack1"
app:itemTextAppearanceActive="@style/Active"
app:itemTextAppearanceInactive="@style/Inactive"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"> </com.google.android.material.bottomnavigation.BottomNavigationView>
  • app:labelVisibilityMode="labeled"   标签显示模式,其实就是改变点选后的整体动画,推荐选中labeled,默认的太恶心了超过3个item就会出现超丑的动画
  • app:itemTextColor="@color/fontBlack1"    item文本的颜色
  • app:itemTextAppearanceActive="@style/Active" 设置选中后的item效果
  • app:itemTextAppearanceInactive="@style/Inactive"  设置未选中的item效果

style

<style name="Active" parent="@style/TextAppearance.AppCompat.Caption">
<item name="android:textSize">@dimen/font_size_17</item>
</style> <style name="Inactive" parent="@style/TextAppearance.AppCompat.Caption">
<item name="android:textSize">@dimen/font_size_11</item>
</style>

只是改变文字大小

menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"> <item
android:id="@+id/home"
android:title="首页"
android:icon="@mipmap/ic_admission"/> <item
android:id="@+id/notice"
android:title="通知"
android:icon="@mipmap/ic_head"/> <item
android:id="@+id/circle"
android:title="圈子"
android:icon="@mipmap/ic_temp"/> <item
android:id="@+id/my_info"
android:title="我的"
android:icon="@mipmap/ic_notice"/> </menu>

图标被Tint颜色覆盖

图标添加后你会发现图标会被Tint颜色覆盖变成灰色的图标,下面这两行代码解决这个问题

        mBottomNavigationView = findViewById(R.id.bottom_navigation_view);
mBottomNavigationView.setItemIconTintList(null);

如果你需要改变选中图标

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="@drawable/ic_home_page_normal"/>
<item android:state_checked="true" android:drawable="@drawable/ic_home_page_selected"/>
</selector>

在menu的item上调用

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

两个点击监听

mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Log.e("ytzn", "onNavigationItemSelected: 选中"+menuItem.getItemId() );
return true;
}
});
mBottomNavigationView.setOnNavigationItemReselectedListener(new BottomNavigationView.OnNavigationItemReselectedListener() {
@Override
public void onNavigationItemReselected(@NonNull MenuItem menuItem) {
Log.e("ytzn", "onNavigationItemSelected: 选中状态再次选中"+menuItem.getItemId() ); }
});
setOnNavigationItemSelectedListener 是点击未选择的item后的回调,返回的boolean是决定是否启用选中效果或者放大效果
setOnNavigationItemReselectedListener 是如果已经是选中状态后,在点击一次后的回调

调整图标和标题的位置


原始的效果, 虽然还可以, 但是我们高标准的设计小妹妹接受不了. 尝试说服她? 不可能的!
这个问题搜了半天也没找到怎么处理的办法. 甚至打算放弃这东西了, 自己实现一个LinearLayout也能把这需求搞定啊, 何苦这么费劲. 但是之前的经历告诉我, 自己实现的话, 需要负责view的状态保存和恢复, 这简直太恶心了.
继续看它的源码实现, 发现原来谷歌的这个东西是完全可以自定制的. 基本上包括所有的ui设置.

在BottomNavigationItemView这个类中, 发现每个item的布局加载:
LayoutInflater.from(context).inflate(layout.design_bottom_navigation_item, this, true);
我们可以自定义这个布局, 替换原始实现, 从而随意的定制自己的UI.
先看看谷歌的这个布局文件是怎么做的:
https://github.com/dandar3/android-support-design/blob/master/res/layout/design_bottom_navigation_item.xml
不列出来的. 然后依葫芦画瓢, 实现自己的一份:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:override="true"> <ImageView
android:id="@+id/icon"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/design_bottom_navigation_margin"
android:layout_marginBottom="@dimen/design_bottom_navigation_margin"
android:duplicateParentState="true" /> <TextView
android:id="@+id/smallLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginBottom="0dp"
android:duplicateParentState="true"
android:text="small" /> <TextView
android:id="@+id/largeLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginBottom="0dp"
android:duplicateParentState="true"
android:text="large"
android:visibility="invisible" />
<!--</FrameLayout>-->
</merge>

而且, 刚才上面提到的字体大小的控制, 也完全可以通过这种方式来实现, 在dimens.xml中自定义一个同名的项目, 替换原来的默认值.

<dimen tools:override="true" name="design_bottom_navigation_text_size">14sp</dimen>
<dimen tools:override="true" name="design_bottom_navigation_active_text_size">14sp</dimen>

好了, 解决了以上几个问题之后, 我的底部tab栏终于实现了.

end

Android开发 BottomNavigationView学习的更多相关文章

  1. Android开发该学习哪些东西?

    开篇: 本人也是众多Android开发道路上行走的一员,听了不少大神的知乎live,自己也看了不少书,也和不少前辈交流过,所以在这里分享一下Android开发应该学习的书籍以及知识,当然,也包括一些方 ...

  2. 【干货推荐】Android开发该学习哪些东西?

    开篇: 本人也是众多Android开发道路上行走的一员,听了不少大神的知乎live,自己也看了不少书,也和不少前辈交流过,所以在这里分享一下Android开发应该学习的书籍以及知识,当然,也包括一些方 ...

  3. Android开发新手学习总结(一)——使用Android Studio搭建Android集成开发环境

    [新手连载]一:使用Android Studio搭建Android集成开发环境http://bbs.itcast.cn/forum.php?mod=viewthread&tid=87055&a ...

  4. android开发的学习路线

    参考资料:千锋3G学院--课程大纲    http://www.mobiletrain.org 看了专业的培训机构的课程大纲,才知道,自己学习android的路途才刚刚开始!特此整理分享一下,希望能帮 ...

  5. Android开发系列----学习伊始

    因为对移动端开发开始感兴趣,开始学习App开发,没有苹果环境的我,只好先选择Android来玩一玩了~~ 找了一套视频,买了几本java.android开发的书,开始搞起~~

  6. Android开发最佳学习路线图

          为了帮助大家更好的学习Android开发的相关知识,尚观4G智能操作系统研究室(www.up4g.com)为大家制作下面学习路线图:希望能帮助到广大的android爱好者. 在開始之前我们 ...

  7. android开发进阶学习博客资源

    Android开发者博客推荐 Android入门级 - 罗宪明 http://blog.csdn.net/wdaming1986 Android入门级 - 魏祝林 http://blog.csdn.n ...

  8. Android开发最佳学习路线图(转)

    Android开发总体路线图:  基础学习——JavaSE:        很多朋友一上手就开始学习Android,似乎太着急了一些. Android应用程序开发是以Java语言为基础的,所以没有扎实 ...

  9. 【随笔】android开发的学习路线

    第一阶段:Java面向对象编程 1.Java基本数据类型与表达式,分支循环. 2.String和StringBuffer的使用.正则表达式. 3.面向对象的抽象,封装,继承,多态,类与对象,对象初始化 ...

随机推荐

  1. hdu多校第五场1005 (hdu6628) permutation 1 排列/康托展开/暴力

    题意: 定义一个排列的差分为后一项减前一项之差构成的数列,求对于n个数的排列,差分的字典序第k小的那个,n<=20,k<=1e4. 题解: 暴力打表找一遍规律,会发现,对于n个数的排列,如 ...

  2. linux在不关机情况下新增硬盘的方法

    开机状态插入硬盘 不关机执行命令 echo "- - -" > /sys/class/scsi_host/host0/scan 注意 echo "- - -&quo ...

  3. taskFactory

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. word 文献标题自动编号

    来自:word中自动编号和多级编号的使用 选中标题或段落,点击鼠标右键,在编号菜单内选择适合的自动编号样式.或者在窗口上方的“开始”选项卡中选择编号样式.如果对已选的编号样式不满意,可以照以上方法直接 ...

  5. Day 11:函数装饰器

    在说装饰器前,先说一个东西,再Python里,有一个 一切皆对象,一切皆变量. 例: def hello(name="sunjinyao"): return "hi &q ...

  6. !important的用法及作用

    定义及语法 !important,作用是提高指定样式规则的应用优先权(优先级).语法格式{ cssRule !important },即写在定义的最后面,例如:box{color:red !impor ...

  7. SpringCloud学习笔记《---06 Config 分布式配置中心---》基础篇

  8. Ubuntu18.04 安装搜狗拼音

    参考文章:https://blog.csdn.net/fx_yzjy101/article/details/80243710 1. 安装fcitx sudo apt-get install fcitx ...

  9. vc枚举本机端口信息

    关于查看本机端口信息,可能大多数人都知道在cmd下的netstat 命令,殊不知该命令在底层也是调用相关api来实现的,相关函数有:GetTcpTableGetExtendedTcpTableGetU ...

  10. Sublime Text最舒服的主题

    Theme - Afterglow (官网链接) 贴上 preferences -> settings 里面的配置 { "theme": "Afterglow-gr ...