An android lib for enhancing BottomNavigationView.
Method | Description |
---|---|
enableAnimation | Enable or disable click item animation(text scale and icon move animation in no item shifting mode). Default true. |
enableItemShiftingMode | Enable the shifting mode for each item. It will has a shift animation for item if true. Otherwise the item text always be shown. Default true when item count > 3. |
enableShiftingMode | Enable the shifting mode for navigation. It will has a shift animation if true. Otherwise all items are the same width. Default true when item count > 3. |
getBottomNavigationItemView | Get private mButton in mMenuView at position |
getBottomNavigationItemViews | Get private mButtons in mMenuView |
getCurrentItem | Get the current checked item position. |
getIconAt | Get icon at position. |
getItemCount | Get item count. |
getItemHeight | Get item height. |
getLargeLabelAt | Get large label at position. Each item has tow label, one is large, another is small. |
getSmallLabelAt | Get small label at position. Each item has tow label, one is large, another is small. |
getMenuItemPosition | Get menu item position in menu. Return position if success, -1 otherwise. |
getOnNavigationItemSelectedListener | Get OnNavigationItemSelectedListener. |
setCurrentItem | Set the current checked item. |
setIconSize | Set all item ImageView size. |
setIconSizeAt | Set all item ImageView size which at position. |
setIconVisibility | Change the visibility of icon. |
setItemHeight | Set menu item height. |
setLargeTextSize | Set all item large TextView size. Each item has tow label, one is large, another is small. Small one will be shown when item state is normal. Large one will be shown when item checked. |
setSmallTextSize | Set all item small TextView size. Each item has tow label, one is large, another is small. Small one will be shown when item state is normal. Large one will be shown when item checked. |
setTextSize | Set all item large and small TextView size. |
setTextVisibility | Change the visibility of text. |
setTypeface | set Typeface for all item TextView. |
setupWithViewPager | This method will link the given ViewPager and this BottomNavigationViewEx together so that changes in one are automatically reflected in the other. This includes scroll state changes and clicks. |
Style
With ViewPager
Add ViewBadger
compileSdkVersion
>= 25
Step 1. Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
Step 2. Add the dependency
compile 'com.github.ittianyu:BottomNavigationViewEx:1.1.7'
Downloading BottomNavigationViewEx.java and copying it to you project。
Adding a custom widget in xml
:
<com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx
android:id="@+id/bnve"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/colorPrimary"
app:itemIconTint="@color/selector_item_color"
app:itemTextColor="@color/selector_item_color"
app:menu="@menu/menu_navigation_with_view_pager" />
Binding view in Activity
:
BottomNavigationViewEx bnve = (BottomNavigationViewEx) findViewById(R.id.bnve);
bnve.enableAnimation(false);
bnve.enableShiftingMode(false);
bnve.enableItemShiftingMode(false);
bnve.setIconSize(widthDp, heightDp);
bnve.setTextSize(sp);
// set adapter
adapter = new VpAdapter(getSupportFragmentManager(), fragments);
bind.vp.setAdapter(adapter);
// binding with ViewPager
bind.bnve.setupWithViewPager(bind.vp);
You can see the demo.
Other usage is the same as official BottomNavigationView
.
You can click here for detail.
If you are using ProGuard you might need to add the following option:
-keep public class android.support.design.widget.BottomNavigationView { *; }
-keep public class android.support.design.internal.BottomNavigationMenuView { *; }
-keep public class android.support.design.internal.BottomNavigationPresenter { *; }
-keep public class android.support.design.internal.BottomNavigationItemView { *; }
The lib is based on BottomNavigationView
in Support Library 25 design
.
I found it was inflexible when I try in demo. For example, I can't change the current checked item by code. So I write a class extends it to provide some useful method.
You no need to worry about stability. Because I minimise modifying by reflecting.
Thanks for Adrián Mouly.
MIT License
Copyright (c) 2017 ittianyu
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
----------
一个增强BottomNavigationView的安卓库。
methods | description |
---|---|
enableAnimation | 开启或关闭点击动画(文字放大效果和图片移动效果)。 默认为 true. |
enableItemShiftingMode | 开始或关闭子菜单位移模式。 如果为 true,除了当前选中项,其他项的文本将会隐藏。 当菜单数大于3时,默认为 true。 |
enableShiftingMode | 开始或关闭导航条位移模式。如果为 true,选中项和其他项的宽度不一样。当菜单数大于3时,默认为 true。 |
getBottomNavigationItemView | 获取位于 position 的私有成员变量 mButton。 |
getBottomNavigationItemViews | 获取私有成员变量 mButtons。 |
getCurrentItem | 获取当前选中项的索引。 |
getIconAt | 获取位于 position 的图片。 |
getItemCount | 获取子项个数。 |
getItemHeight | 获取菜单高度。 |
getLargeLabelAt | 获取位于 position 的大标签. 每个子项包含两个标签,一个大的,一个小的。 |
getSmallLabelAt | 获取位于 position 的小标签. 每个子项包含两个标签,一个大的,一个小的。 |
getMenuItemPosition | 获取子菜单的索引。如果找不到,返回 -1。 |
getOnNavigationItemSelectedListener | 获取 OnNavigationItemSelectedListener。 |
setCurrentItem | 设置当前选中项。 |
setIconSize | 设置所有的子项图标大小。 |
setIconSizeAt | 设置位于 position 的图标的大小。 |
setIconVisibility | 设置图片可见性。 |
setItemHeight | 设置子项高度。 |
setLargeTextSize | 设置所有子项的大标签文本大小。每个子项有两个标签,一个大的,一个小的。当子项未选中时,显示小标签;选中时,显示大标签。 |
setSmallTextSize | 设置所有子项的小标签文本大小。每个子项有两个标签,一个大的,一个小的。当子项未选中时,显示小标签;选中时,显示大标签。 |
setTextSize | 设置所有子项的大和小标签文本大小。 |
setTextVisibility | 设置文本可见性。 |
setTypeface | 设置所有子项的 TextView 字体 |
setupWithViewPager | 和 ViewPager 绑定,当 任何一个选中项改变时,都会自动改变另一项。 |
样式
和 ViewPager 一起使用
带数字的小红圈
compileSdkVersion
>= 25
步骤 1. 在工程根目录的 build.gradle
最后添加如下代码:
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
步骤 2. 添加依赖
compile 'com.github.ittianyu:BottomNavigationViewEx:1.1.7'
下载文件 BottomNavigationViewEx.java 并复制到你的工程中。
在 xml
布局中添加自定义控件:
<com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx
android:id="@+id/bnve"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/colorPrimary"
app:itemIconTint="@color/selector_item_color"
app:itemTextColor="@color/selector_item_color"
app:menu="@menu/menu_navigation_with_view_pager" />
在 Activity
中绑定控件:
BottomNavigationViewEx bnve = (BottomNavigationViewEx) findViewById(R.id.bnve);
bnve.enableAnimation(false);
bnve.enableShiftingMode(false);
bnve.enableItemShiftingMode(false);
bnve.setIconSize(widthDp, heightDp);
bnve.setTextSize(sp);
// set adapter
adapter = new VpAdapter(getSupportFragmentManager(), fragments);
bind.vp.setAdapter(adapter);
// binding with ViewPager
bind.bnve.setupWithViewPager(bind.vp);
请参考demo。
其他用法和官方 BottomNavigationView
一样。
详情点击这里
如果你启用了 ProGuard,那你应该加上以下混淆代码:
-keep public class android.support.design.widget.BottomNavigationView { *; }
-keep public class android.support.design.internal.BottomNavigationMenuView { *; }
-keep public class android.support.design.internal.BottomNavigationPresenter { *; }
-keep public class android.support.design.internal.BottomNavigationItemView { *; }
本库修改自安卓官方 Support Library 25 design
中的 BottomNavigationView
。
我在尝试使用官方的库时,发现缺少灵活性。比如官方并没有提供切换当前选中项的方法。所以我在此基础上包装了一层,对外公开了一些方法。
你完全没有必要担心库的稳定性,因为我是使用反射对父类进行最小限度的修改。
感谢 Adrián Mouly.
MIT License
Copyright (c) 2017 ittianyu
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.