Android开发 BottomNavigationView学习
前言
注意这个里介绍的是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学习的更多相关文章
- Android开发该学习哪些东西?
开篇: 本人也是众多Android开发道路上行走的一员,听了不少大神的知乎live,自己也看了不少书,也和不少前辈交流过,所以在这里分享一下Android开发应该学习的书籍以及知识,当然,也包括一些方 ...
- 【干货推荐】Android开发该学习哪些东西?
开篇: 本人也是众多Android开发道路上行走的一员,听了不少大神的知乎live,自己也看了不少书,也和不少前辈交流过,所以在这里分享一下Android开发应该学习的书籍以及知识,当然,也包括一些方 ...
- Android开发新手学习总结(一)——使用Android Studio搭建Android集成开发环境
[新手连载]一:使用Android Studio搭建Android集成开发环境http://bbs.itcast.cn/forum.php?mod=viewthread&tid=87055&a ...
- android开发的学习路线
参考资料:千锋3G学院--课程大纲 http://www.mobiletrain.org 看了专业的培训机构的课程大纲,才知道,自己学习android的路途才刚刚开始!特此整理分享一下,希望能帮 ...
- Android开发系列----学习伊始
因为对移动端开发开始感兴趣,开始学习App开发,没有苹果环境的我,只好先选择Android来玩一玩了~~ 找了一套视频,买了几本java.android开发的书,开始搞起~~
- Android开发最佳学习路线图
为了帮助大家更好的学习Android开发的相关知识,尚观4G智能操作系统研究室(www.up4g.com)为大家制作下面学习路线图:希望能帮助到广大的android爱好者. 在開始之前我们 ...
- android开发进阶学习博客资源
Android开发者博客推荐 Android入门级 - 罗宪明 http://blog.csdn.net/wdaming1986 Android入门级 - 魏祝林 http://blog.csdn.n ...
- Android开发最佳学习路线图(转)
Android开发总体路线图: 基础学习——JavaSE: 很多朋友一上手就开始学习Android,似乎太着急了一些. Android应用程序开发是以Java语言为基础的,所以没有扎实 ...
- 【随笔】android开发的学习路线
第一阶段:Java面向对象编程 1.Java基本数据类型与表达式,分支循环. 2.String和StringBuffer的使用.正则表达式. 3.面向对象的抽象,封装,继承,多态,类与对象,对象初始化 ...
随机推荐
- 802.11ac wave2的前世今生
2015年下半年,高通.博通.RTL等芯片厂商相继发布了满足802.11ac wave2要求的芯片,WLAN及终端厂商也迅速跟进推出相应的产品和终端.802.11ac wave2在多方推动下于2015 ...
- spring boot资源文件配置读取
一般业务配置,尽量新建自己的配置文件,来读取,而不是配置在application.properties或application-*.properties或yml/yaml配置中. applicatio ...
- java-day13
异常 指的是程序在执行过程中,出现的非正常情况,最终会导致JVM的非正常停止 异常分类:编译异常,运行期异常 异常的产生过程分析 throw关键字:指方法中抛出指定异常 使用格式:throw new ...
- 【HDUOJ】1213 How many tables
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意:Ignatius邀请了n个朋友来家里,朋友之间如果互相不认识的不想坐一起,所以至少要准备几 ...
- spring在普通类中获取session和request
在使用spring时,经常需要在普通类中获取session,request等对像.比如一些AOP拦截器类,在有使用struts2时,因为struts2有一个接口使用org.apache.struts2 ...
- Java oop第05章_多态、接口
一. 为什么需要接口? Java中当一个类继承父类时,最多也只能继承一个父类,扩展性会受到一定的限制,为了弥补这一缺陷,Java又提供了一种新的引用数据类型分类,叫接口. 一个类只能继承一个父类,但可 ...
- shell 命令 链接文件 ln
1. 创建软链接文件 [ ln -s 源文件 链接文件] 类似于windows下的快捷方式 [ ln -s expr.sh expr ] 此时创建的链接文件路径是相对于现在所在的路径 ...
- python库之lightgbm
一.安装 https://blog.csdn.net/qq_40317897/article/details/81021958 参考文献: [1].LightGBM中文文档 https://light ...
- amaze UI(mark)
为移动而生 Amaze UI 以移动优先(Mobile first)为理念,从小屏逐步扩展到大屏,最终实现所有屏幕适配,适应移动互联潮流. 组件丰富,模块化 Amaze UI 含近 20 个 CSS ...
- BlueHost主机建站方案怎样选择?
BlueHost是知名美国主机商,近年来BlueHost不断加强中国市场客户的用户体验,提供多种主机租用方案,基本能够满足各类网站建设需求.下面就和大家介绍一下建站应该怎样选择主机. 1.中小型网站 ...