Android学习之BottomNavigationBar实现Android特色底部导航栏

原文地址:http://blog.csdn.net/qq_16131393/article/details/51419901

感谢作者提供博文

下面是我的总结。

实现:

1、添加依赖

compile 'com.ashokvarma.android:bottom-navigation-bar:2.0.3'
2、写入布局文件

<com.ashokvarma.bottomnavigation.BottomNavigationBar
    android:id="@+id/bnb"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:textAlignment="center"
    />
3、控件绑定并设置text和icon

控件绑定就不说了,直接贴代码

private BottomNavigationBar mBottomNavigationBar;
mBottomNavigationBar = findViewById(R.id.bnb);
//设置BottomNavigationBar的mode
mBottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
//设置BottomNavigationBar的样式style
mBottomNavigationBar
        .setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC
        );
mBottomNavigationBar.addItem(new BottomNavigationItem(R.drawable.ic_home_white_24dp, "首页").setActiveColorResource(R.color.orange))
        .addItem(new BottomNavigationItem(R.drawable.legal, "法律知识").setActiveColorResource(R.color.teal))
        .addItem(new BottomNavigationItem(R.drawable.demand, "需求").setActiveColorResource(R.color.blue))
        .addItem(new BottomNavigationItem(R.drawable.setting, "设置").setActiveColorResource(R.color.colorPrimary))
        .setFirstSelectedPosition(0)
        .initialise();

mBottomNavigationBar.setTabSelectedListener(this);
特别注意:setMode()和setBackgroundstyle()一定要放在addItem()之前,要不没有想要的效果

bottomNavigationBar设置监听选项点击事件setTabSelectedListener。

//以下三个是bottomNavigationBar的监听
@Override
public void onTabSelected(int position) {
    switch (position) {
        case 0:
            showHomeFragment();
            break;
        case 1:
            showLegalAidKnowledgeFragment();
            break;
        case 2:
            showDemandFragment();
            break;
        case 3:
            showSettingFragment();
            break;
        default:
            break;
    }

}


@Override
public void onTabUnselected(int position) {

}

@Override
public void onTabReselected(int position) {

}

设置导航栏模式

Attribute: bnbMode Values: mode_default, mode_fixed, mode_shifting 
Method: setMode() Values:MODE_DEFAULT, MODE_FIXED, MODE_SHIFTING

//设置BottomNavigationBar的mode
mBottomNavigationBar.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的样式style
mBottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC
注意:导航栏的模式和背景模式搭配不一下,效果也是不一样的。

颜色设置

Attributes: bnbActiveColor, bnbInactiveColor, bnbBackgroundColor Value: Color value or resource 
Methods: setActiveColor, setInActiveColor, setBarBackgroundColor Value: Color value or resource

mBottomNavigationBar.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.白色是背景色

代码示例:

mBottomNavigationBar.addItem(new BottomNavigationItem(R.drawable.ic_home_white_24dp, "首页").setActiveColorResource(R.color.orange))
        .addItem(new BottomNavigationItem(R.drawable.legal, "法律知识").setActiveColorResource(R.color.teal))
        .addItem(new BottomNavigationItem(R.drawable.demand, "需求").setActiveColorResource(R.color.blue))
        .addItem(new BottomNavigationItem(R.drawable.setting, "设置").setActiveColorResource(R.color.colorPrimary))
        .setFirstSelectedPosition(0)
        .initialise();
添加标记:

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, "首页").setActiveColorResource(R.color.orange).setBadgeItem(numberBadgeItem));

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值