Android学习之BottomNavigationBar实现Android特色底部导航栏
Android底部导航栏的实现方式特别多,例如TabHost,TabLayout,或者TextView等,都可以实现底部导航栏的效果,但是却没有Google官方统一的导航栏样式,今天讲的就是Google最近添加到Material design中的底部导航栏BottomNavigationBar,也可以说是现今Android底部导航栏的一个标准与统一吧。
效果:
实现效果:
实现:
1.下载jar包
2.添加Maven
<dependency><groupId>com.ashokvarma.android</groupId><artifactId>bottom-navigation-bar</artifactId><version>1.2.0</version><type>pom</type>
</dependency>
3.添加依赖。
compile 'com.ashokvarma.android:bottom-navigation-bar:1.2.0'
4.添加Ivy
<dependency org='com.ashokvarma.android' name='bottom-navigation-bar' rev='1.2.0'><artifact name='$AID' ext='pom'></artifact>
</dependency>
布局设置
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.example.wangchang.testbottomnavigationbar.MainActivity"><FrameLayout
android:id="@+id/layFrame"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1" /><com.ashokvarma.bottomnavigation.BottomNavigationBar
android:id="@+id/bottom_navigation_bar"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="bottom" />
</LinearLayout>
这个很简单,不用详叙。
主页面实现:
package com.example.wangchang.testbottomnavigationbar;import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;import java.util.ArrayList;public class MainActivity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener {private ArrayList<Fragment> fragments;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);BottomNavigationBar bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);bottomNavigationBar.addItem(new BottomNavigationItem(R.mipmap.ic_home_white_24dp, "Home").setActiveColorResource(R.color.orange)).addItem(new BottomNavigationItem(R.mipmap.ic_book_white_24dp, "Books").setActiveColorResource(R.color.teal)).addItem(new BottomNavigationItem(R.mipmap.ic_music_note_white_24dp, "Music").setActiveColorResource(R.color.blue)).addItem(new BottomNavigationItem(R.mipmap.ic_tv_white_24dp, "Movies & TV").setActiveColorResource(R.color.brown)).addItem(new BottomNavigationItem(R.mipmap.ic_videogame_asset_white_24dp, "Games").setActiveColorResource(R.color.grey)).setFirstSelectedPosition(0).initialise();fragments = getFragments();setDefaultFragment();bottomNavigationBar.setTabSelectedListener(this);}/*** 设置默认的*/private void setDefaultFragment() {FragmentManager fm = getSupportFragmentManager();FragmentTransaction transaction = fm.beginTransaction();transaction.replace(R.id.layFrame, HomeFragment.newInstance("Home"));transaction.commit();}private ArrayList<Fragment> getFragments() {ArrayList<Fragment> fragments = new ArrayList<>();fragments.add(HomeFragment.newInstance("Home"));fragments.add(BookFragment.newInstance("Books"));fragments.add(MusicFragment.newInstance("Music"));fragments.add(TvFragment.newInstance("Movies & TV"));fragments.add(GameFragment.newInstance("Games"));return fragments;}@Overridepublic void onTabSelected(int position) {if (fragments != null) {if (position < fragments.size()) {FragmentManager fm = getSupportFragmentManager();FragmentTransaction ft = fm.beginTransaction();Fragment fragment = fragments.get(position);if (fragment.isAdded()) {ft.replace(R.id.layFrame, fragment);} else {ft.add(R.id.layFrame, fragment);}ft.commitAllowingStateLoss();}}}@Overridepublic void onTabUnselected(int position) {if (fragments != null) {if (position < fragments.size()) {FragmentManager fm = getSupportFragmentManager();FragmentTransaction ft = fm.beginTransaction();Fragment fragment = fragments.get(position);ft.remove(fragment);ft.commitAllowingStateLoss();}}}@Overridepublic void onTabReselected(int position) {}
}
这里主要注意一下Fragment的填充方式,静态工厂构造方法。
bottomNavigationBar.addItem(new BottomNavigationItem(R.drawable.ic_home_white_24dp, "Home")).addItem(new BottomNavigationItem(R.drawable.ic_book_white_24dp, "Books")).addItem(new BottomNavigationItem(R.drawable.ic_music_note_white_24dp, "Music")).addItem(new BottomNavigationItem(R.drawable.ic_tv_white_24dp, "Movies & TV")).addItem(new BottomNavigationItem(R.drawable.ic_videogame_asset_white_24dp, "Games")).initialise();
bottomNavigationBar添加选项。
bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener(){@Overridepublic void onTabSelected(int position) {}@Overridepublic void onTabUnselected(int position) {}@Overridepublic void onTabReselected(int position) {}});
bottomNavigationBar设置监听选项点击事件setTabSelectedListener。
设置导航栏模式
Attribute: bnbMode Values: mode_default, mode_fixed, mode_shifting
Method: setMode() Values:MODE_DEFAULT, MODE_FIXED, MODE_SHIFTING
分别是属性或者代码设置
bottomNavigationBar
.setMode(BottomNavigationBar.MODE_FIXED);
设置导航栏背景模式
Attribute: background_style_default, background_style_static, background_style_ripple
Method:BACKGROUND_STYLE_DEFAULT,BACKGROUND_STYLE_STATIC, BACKGROUND_STYLE_RIPPLE
bottomNavigationBar .setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_RIPPLE)
MODE_FIXED+BACKGROUND_STYLE_STATIC效果
MODE_FIXED+BACKGROUND_STYLE_RIPPLE效果
MODE_SHIFTING+BACKGROUND_STYLE_STATIC效果
MODE_SHIFTING+BACKGROUND_STYLE_RIPPLE效果
值得一提,模式跟背景的设置都要在添加tab前面,不然不会有效果。
颜色设置
Attributes: bnbActiveColor, bnbInactiveColor, bnbBackgroundColor Value: Color value or resource
Methods: setActiveColor, setInActiveColor, setBarBackgroundColor Value: Color value or resource
bottomNavigationBar
.setActiveColor(R.color.primary)
.setInActiveColor("#FFFFFF")
.setBarBackgroundColor("#ECECEC")
in-active color : is the icon and text color of the in-active/un-selected tab
是图标和文字的颜色(选中/未选中)
active color : In BACKGROUND_STYLE_STATIC active color is the icon and text color of the active/selected tab. In BACKGROUND_STYLE_RIPPLE active color is the bottom bar background color (which comes with ripple animation)
在BACKGROUND_STYLE_STATIC 模式下颜色是图标和文字的颜色(选中/未选中),在BACKGROUND_STYLE_RIPPLE 模式下是底部导航栏背景色。
background color : In BACKGROUND_STYLE_STATIC background color is the bottom bar background color. In BACKGROUND_STYLE_RIPPLE background color is the icon and text color of the active/selected tab.
在BACKGROUND_STYLE_STATIC 模式下颜色是底部导航栏背景色,BACKGROUND_STYLE_RIPPLE模式下是图标和文字的颜色(选中/未选中)
默认颜色:
Default colors:
1. Theme’s Primary Color will be active color.
2. Color.LTGRAY will be in-active color.
3. Color.WHITE will be background color.
1.主题的PrimaryColor将是active color
2.Color.LTGRAY(灰色)是 in-active color
3.白色是背景色
代码示例:
bottomNavigationBar.addItem(new BottomNavigationItem(R.drawable.ic_home_white_24dp, "Home").setActiveColor(R.color.orange).setInActiveColor(R.color.teal)).addItem(new BottomNavigationItem(R.drawable.ic_book_white_24dp, "Books").setActiveColor("#FFFF00")).addItem(new BottomNavigationItem(R.drawable.ic_music_note_white_24dp, "Music").setInActiveColor("#00FFFF")).addItem(new BottomNavigationItem(R.drawable.ic_tv_white_24dp, "Movies & TV")).addItem(new BottomNavigationItem(R.drawable.ic_videogame_asset_white_24dp, "Games").setActiveColor(R.color.grey))
添加标记
BadgeItem numberBadgeItem = new BadgeItem().setBorderWidth(4).setBackgroundColorResource(R.color.blue).setText("5").setHideOnSelect(autoHide.isChecked());bottomNavigationBar.addItem(new BottomNavigationItem(R.drawable.ic_home_white_24dp, "Home").setActiveColorResource(R.color.orange).setBadgeItem(numberBadgeItem))
效果:
很棒吧,功能很全,效果很nice!
代码下载:
http://pan.baidu.com/s/1eR4DzGY
github地址:
https://github.com/Ashok-Varma/BottomNavigation
Android学习之BottomNavigationBar实现Android特色底部导航栏相关推荐
- Android获取顶部状态栏statusBar高度、底部导航栏navigationBar高度
手机顶部显示时间.电量等信息的叫状态栏,即statusBar 有些手机比如华为在底部会有返回.回到主页面等虚拟功能键,这是导航栏,即navigationBar 下面列出一些项目中常用的功能代码? 嘻嘻 ...
- Android 最常用的四种底部导航栏实现
Android 底部导航栏实现 底部导航栏的应用非常的广泛.今天就总结下几种实现的方式. 一.Fragment + TextView 实现 前面一篇博客在介绍Fragment 的时候就使用了Fragm ...
- JFTabBar android强大的底部导航栏框架 (微信底部导航栏效果)
TabBar这个名字相信很多学过一点IOS程序员都知道它是用来干嘛的,但本人也并非擅长开发IOS程序员,只是略懂略懂....这是一个很强大的TabBar,可满足很多需求.用起来也非常简单,在oncre ...
- 【Flutter】底部导航栏实现 ( BottomNavigationBar 底部导航栏 | BottomNavigationBarItem 导航栏条目 | PageView )
文章目录 一.Scaffold 组件 二.底部导航栏整体架构 三.BottomNavigationBar 底部导航栏 四.BottomNavigationBarItem 导航栏条目 五.PageVie ...
- Flutter仿闲鱼底部导航栏实现
概述 本文主要实现一个仿闲鱼底部导航栏实现,这种效果在项目中经常用到,接下来我们就从头开始实现. 仿闲鱼底部导航栏 要实现闲鱼底部导航栏的效果我们需要使用到BottomNavigationBar和Fl ...
- 21天学习之二(Android 10.0 SystemUI默认去掉底部导航栏的三种方法)
活动地址:CSDN21天学习挑战赛 1.概述 在定制化开发中,在SystemUI的一些定制功能中,针对默认去掉底部导航栏的方法有好几种,StatusBar和DisplayPolicy.java中api ...
- Android 底部导航栏 BottomNavigationBar
大部分app,都采用导航栏的方式,无论是顶部导航栏还是底部导航栏,这是一个app的根骨,本文是采用Google最近添加到Material design中的底部导航栏BottomNavigationBa ...
- android底部导航栏中间凸起,Flutter学习笔记--仿闲鱼底部导航栏带有中间凸起图标...
仿闲鱼底部导航栏带有中间凸起图标 刚接触Flutter,需要实现一个类似闲鱼APP的底部导航栏的实现 要实现的效果如图: 好的,下面开始上代码了: 一. 在main.dart文件中,定义APP的入口信 ...
- 闲鱼底部tab android,Flutter学习笔记--仿闲鱼底部导航栏带有中间凸起图标
image 霸图镇楼 仿闲鱼底部导航栏带有中间凸起图标 需求情景: 需要实现一个类似闲鱼APP的底部导航栏的实现 要实现的效果如图 image 好的,下面开始上代码了: 一. 在main.dart文件 ...
最新文章
- 图解Java内存区域及内存溢出异常
- Google Map二次开发——API方式
- java jar包与配置文件的写法
- C语言课后习题(23)
- textaligncenter仍然不居中_三星Galaxy S21/S21+保护膜曝光:居中挖孔 回归直屏_手机行情...
- pip install安装php,Python下的常用下载安装工具pip的安装方法
- Find the longest route with the smallest starting point
- chromium中的性能优化工具syzyProf
- 5.0在python中是一个整数常量_python学习之路,基础篇-变量和常量
- 枯燥的Kotlin协程三部曲(中)——应用实战篇
- 为什么DDR电源设计时需要VTT电源?
- 【微信小程序】合法域名校验出错,不在以下合法域名列表中 解决方法
- DDD到底是不是毒瘤?
- 什么是时间复杂度与空间复杂度
- STM32单片机---外部中断
- 执行HiveSql时报错“FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.mr.MapRedTa”
- 一个有意思的404页面
- Netflix创始人:我不要求996,照样市值万亿!
- 最新全球学术排名出炉:23所中国大学跻身世界前100名!
- mysql实现年龄大于17小于30_mysql基础