Android Material Design 之 BottomNavigationView
来源:互联网 发布:行装软件 编辑:程序博客网 时间:2024/06/04 17:52
概述
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
- Android Material Design 之 BottomNavigationView
- Android 之 Material Design(二)—BottomNavigationView
- Material Design之导航栏BottomNavigationView的使用
- Android Material Design中的BottomNavigationView实现底部导航栏
- Material Design系列-- BottomNavigationView(转载)
- Android Design Library之二: BottomNavigationView
- Android Material Design 之 Material Theme
- Android:Material Design之 Switch
- Android Material Design 之 基础知识
- Android Material Design 之 图形
- android material design之snackbar
- Android Material Design之BottomSheetDialog
- Android Material Design之TextInputLayout
- Android Material Design之Snackbar
- Android Material Design之FloatingActionButton
- Android Material Design之CoordinatorLayout
- Android Material Design之TextInputLayout
- 基础Android之Material Design
- mysql数据库连接的最原始的方式
- Zigbee之TICC2530开发笔记(1)
- 【贪心】CodeChef(PROTEPOI)[Protecting The Poison]题解
- Bootstrap Table 实现简单的查询和显示功能
- Java集合类框架汇总
- Android Material Design 之 BottomNavigationView
- hdu6070 Dirt Ratio (线段树:二分+多次建树+构造难想)
- Visual Stdio 2017 无法找到资源编译器rcdll.dll
- C语言基础程序汇总
- html/CSS笔试习题总结1
- Spark入门(三):RDD概述
- leetcode: 76. Minimum Window Substring
- Hadoop基础教程-第11章 Hive:SQL on Hadoop(11.6 HQL:DML数据操纵)(草稿)
- 每日MySQL之019:使用SHOW命令查看数据库信息