Android底部导航BottomNavigationBar

项目地址:

https://github.com/Ashok-Varma/BottomNavigation

参考

1.http://www.jianshu.com/p/0550500f8f56

2.http://blog.csdn.net/qq_16131393/article/details/51419901

1、样式

  • 底部导航的action通常是3-5个,小于3个推荐使用tab,大于三个推荐使用navigation drawer
  • 在切换的时候,使用cross-fade动画,避免视图间的横向移动

2、Fixed、Shifting

Bottom navigation,有两种Mode:分别是Fixed、Shifting

Fixed:

Shifting:

3、代码实现

1.依赖:

compile ‘com.ashokvarma.android:bottom-navigation-bar:1.4.1‘

2.布局文件:

<com.ashokvarma.bottomnavigation.BottomNavigationBar
        android:id="@+id/bottom_navigation_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"/>
        <!--app:bnbAutoHideEnabled="false"/>-->

3、Activity中的实现,注意的参数

1.设置导航栏模式:setMode()

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

.setMode(BottomNavigationBar.MODE_FIXED);

2.设置导航栏背景模式:setBackgroundStyle()

Attribute: background_style_static, background_style_ripple,background_style_default

Method:setBackgroundStyle()Values:BACKGROUND_STYLE_STATIC, BACKGROUND_STYLE_RIPPLE

.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_RIPPLE)      

3.设置BottomNavigationItem颜色

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

BottomNavigationItem
.setActiveColorResource(R.color.primary)

4.添加标记BadgeItem

 BadgeItem numberBadgeItem = new BadgeItem()
                .setBorderWidth(4)
                .setBackgroundColorResource(R.color.blue)
                .setText("5")
                .setHideOnSelect(autoHide.isChecked());  // TODO 控制便签消失或不消失

     bottomNavigationBar
                .addItem(new BottomNavigationItem(R.drawable.ic_home_white_24dp, "Home").setActiveColorResource(R.color.orange).setBadgeItem(numberBadgeItem)) // TODO 设置 BadgeItem 默认隐藏 注意 这句代码在添加 BottomNavigationItem 之后 numberBadgeItem.hide();  

5.设置事件监听器TabChangeListener

 bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener(){
            @Override
            public void onTabSelected(int position) {
            }
            @Override
            public void onTabUnselected(int position) {]
            }
            @Override
            public void onTabReselected(int position) {
            }
        });

展示

MODE_FIXED+BACKGROUND_STYLE_STATIC效果

DE_FIXED+BACKGROUND_STYLE_RIPPLE效果

MODE_SHIFTING+BACKGROUND_STYLE_STATIC效果

MODE_SHIFTING+BACKGROUND_STYLE_RIPPLE效果

案例的实现

JAVA

public class MainActivity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener {

    @BindView(R.id.bottom_navigation_bar)
    BottomNavigationBar mBottomNavigationBar;
    @BindView(R.id.fragment_container)
    FrameLayout mFrameLayout;
    private BookFragment mBookFragment;
    private LikeFragment mLikeFragment;
    private UserFragment mUserFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        InitNavigationBar();

    }

    private void InitNavigationBar() {
        mBottomNavigationBar.setTabSelectedListener(this);
        mBottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        mBottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_RIPPLE);
        mBottomNavigationBar
                .addItem(new BottomNavigationItem(R.drawable.literature_100, "图书").setActiveColorResource(R.color.orange))
                .addItem(new BottomNavigationItem(R.drawable.like_100, "收藏").setActiveColorResource(R.color.teal))
                .addItem(new BottomNavigationItem(R.drawable.user_100, "设置").setActiveColorResource(R.color.blue))
                .setFirstSelectedPosition(0)
                .initialise();
    }

    /**
     * 设置默认的
     */
    private void setDefaultFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        mBookFragment = BookFragment.newInstance();
        transaction.replace(R.id.fragment_container, mBookFragment);
        transaction.commit();
    }

    @Override
    public void onTabSelected(int position) {
        Log.d("onTabSelected", "onTabSelected: " + position);
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        switch (position) {
            case 0:
                if (mBookFragment == null) {
                    mBookFragment = BookFragment.newInstance();
                }
                transaction.replace(R.id.fragment_container, mBookFragment);
                break;
            case 1:
                if (mLikeFragment == null) {
                    mLikeFragment = LikeFragment.newInstance();
                }
                transaction.replace(R.id.fragment_container, mLikeFragment);
                break;
            case 2:
                if (mUserFragment == null) {
                    mUserFragment = UserFragment.newInstance();
                }
                transaction.replace(R.id.fragment_container, mUserFragment);
                break;
            default:
                break;
        }
        // 事务提交
        transaction.commit();
    }

    @Override
    public void onTabUnselected(int position) {
        Log.d("onTabUnselected", "onTabUnselected: " + position);
    }

    @Override
    public void onTabReselected(int position) {
        Log.d("onTabReselected", "onTabReselected: " + position);
    }
}

XML

<?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.ryan.simplereading.MainActivity">

    <FrameLayout
        android:id="@+id/fragment_container"
        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>
时间: 2024-12-15 07:14:19

Android底部导航BottomNavigationBar的相关文章

android 底部导航总结

实现android 底部导航的方式有好几种如:fragment:.TabActivity.ViewGroup.viewPager等, 这里介绍使用viewPager实现底部导航. 先说说使用viewPager实现的原因 1.fragment a  我们需要使用fragment的话,我们只能用v4包里面的fragment,因为我们需要向下兼容. b  当我们用V4包里面的fragment的时候,系统会自动添加一个节点. c  fragment不允许嵌套fragmentd  这个谷歌是在4.2里面才

Android底部导航栏

Android UI-仿微信底部导航栏布局 Android基础入门教程——5.2.1 Fragment实例精讲——底部导航栏的实现(方法1)

Android 底部导航 UI框架

此版本在"一个不错的UI框架"基础上修改了首次启动底部导航没有选中Tab的情况 运行效果图如下: 1.TabView.java 这里修改的是mState=-1 /******************************************************************************* * * Copyright (c) Weaver Info Tech Co. Ltd * * TabView * * app.ui.widget.TabView.jav

Android 底部导航栏的使用 fragment

一.初始化各项组件 private void initViews() { viewPager = (ViewPager) findViewById(R.id.view_pager); imgDynamic = (ImageView) findViewById(R.id.tab_dynamic_get); imgMsg = (ImageView) findViewById(R.id.tab_msg_get); imgExam = (ImageView) findViewById(R.id.tab_

Android 底部导航菜单

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="65.32999dip" android:layout

Android底部导航中间突起

看到很多应用下面都有一些特别突出的导航菜单,中间凸起的那种,自己分享一个项目里的吧!如下图: 源码如下: 点击打开链接 版权声明:本文为博主原创文章,未经博主允许不得转载.

Android 底部导航栏的xml

<?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" xmlns:tools="http://schemas.android

底部导航栏使用BottomNavigationBar

1.github地址 https://github.com/zhouxu88/BottomNavigationBar 2.基本使用 2,1添加依赖 implementation 'com.ashokvarma.android:bottom-navigation-bar:2.0.5' 2.2布局中使用 <com.ashokvarma.bottomnavigation.BottomNavigationBar android:layout_width="match_parent" an

Xamarin.Forms 3.1.0+版本 Android 原生支持底部导航栏啦

Xamarin.Forms 3.1.0+版本 Android 原生支持底部导航栏啦 Xamarin.Forms 3.1.0以上版本终于支持Android底部导航栏啦,可以不用第三方的支持库了. https://developer.xamarin.com/releases/xamarin-forms/xamarin-forms-3.1/3.1.0/ C#代码: On<Android>().SetToolbarPlacement(ToolbarPlacement.Bottom); Xaml代码: