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.面向对象的抽象,封装,继承,多态,类与对象,对象初始化 ...
随机推荐
- NX二次开发-NXOPEN将工程图转成PDF文件
NX9+VS2012 #include <uf.h> #include <uf_draw.h> #include <NXOpen/PrintPDFBuilder.hxx& ...
- Notepad++如何对比文件 Notepad++对比两个文件代码方法
大家在使用Notepad++的时候,需要对编辑的两个文件进行比较,找出两个文件代码的区别,快速进行编辑修改,那么Notepad++如何对比文件,下面小编就给大家带来Notepad++对比两个文件代码方 ...
- python3 线程 threading.Thread GIL性能详解(2.3)
python3 线程 threading 最基础的线程的使用 import threading, time value = 0 lock = threading.Lock() def change(n ...
- beautifulsoup的使用
解析库 解析器 使用方法 优势 劣势 Python标准库 BeautifulSoup(markup, "html.parser") Python的内置标准库.执行速度适中 .文档容 ...
- 【转】详解tomcat的连接数与线程池
对tomcat线程池.Connector的BIO.NIO解析的很透彻的一篇文章. 原文链接:https://www.cnblogs.com/kismetv/p/7806063.html 前言 在使用t ...
- 2019-10-9-dotnet-不申请额外数组空间合并多个只读数组列表
title author date CreateTime categories dotnet 不申请额外数组空间合并多个只读数组列表 lindexi 2019-10-09 15:15:10 +0800 ...
- 获取linux性能数据
import reimport osimport sysimport jsonimport socketfrom urllib import request,parseimport urllibfro ...
- 2019 Multi-University Training Contest 3 B 支配树
题目传送门 题意:给出衣服有向无环图(DAG),,定义出度为0的点为中心城市,每次询问给出两个点,求破坏任意一个城市,使得这两个点至少有一个点无法到达中心城市,求方案数. 思路:首先建立反向图,将城市 ...
- react添加多个域名proxy代理,跨域
在package.json中加入如下: { "name": "demo", "version": "0.1.0", &q ...
- leetcode-11-盛水最多的容器
题目描述: 方法一:双指针 class Solution: def maxArea(self, height: List[int]) -> int: left = 0 right = len(h ...