先来张效果图

接下来是实现过程

1.加入依赖

compile 'com.ashokvarma.android:bottom-navigation-bar:1.3.0'

2.xml布局

 fragment.xml的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"> <TextView
android:id="@+id/tv_fragment_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/app_name"
android:textColor="@color/colorAccent"
android:textSize="24sp"/>
</LinearLayout>
 activity_main.xml的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="cct.buttomnavigationbar.MainActivity">
<LinearLayout
android:id="@+id/ll_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:text="Hello World!">
</LinearLayout>
<com.ashokvarma.bottomnavigation.BottomNavigationBar
android:id="@+id/bottom_navigation_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
</com.ashokvarma.bottomnavigation.BottomNavigationBar>
</RelativeLayout>
 colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="green">#79E65D</color>
<color name="orange">#F8A52A</color>
<color name="pink">#FF77FF</color>
<color name="yellow">#FFFF44</color>
<color name="light">#BBFFF1</color>
</resources>
 strings.xml
<resources>
<string name="app_name">BottomNavigationBar</string>
<string name="tab_one">Fish</string>
<string name="tab_two">Fly</string>
<string name="tab_three">Bird</string>
<string name="tab_four">Coffee</string>
</resources>

2.Java代码

BaseFragment类

 package cct.buttomnavigationbar.fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import cct.buttomnavigationbar.R;
public class BaseFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment,container,false);
TextView mFragmentText = (TextView) view.findViewById(R.id.tv_fragment_text);
Bundle bundle = getArguments();
String args = bundle.getString(Constants.KEY_ARGS);
mFragmentText.setText(args);
return view;
}
}

Constants类

 package cct.buttomnavigationbar.fragment;
public class Constants {
public static final String KEY_ARGS = "args";
}

Fragment1类, Fragment2,Fragment3,Fragment4与之一样,就不贴了

 package cct.buttomnavigationbar.fragment;
import android.os.Bundle;
public class FragmentOne extends BaseFragment {
public static FragmentOne newInstance(String s){
Bundle bundle = new Bundle();
bundle.putString(Constants.KEY_ARGS,s);
FragmentOne fragment = new FragmentOne();
fragment.setArguments(bundle);
return fragment;
}
}

MainActivity类

 package cct.buttomnavigationbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.ashokvarma.bottomnavigation.BadgeItem;
import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import cct.buttomnavigationbar.fragment.FragmentFour;
import cct.buttomnavigationbar.fragment.FragmentOne;
import cct.buttomnavigationbar.fragment.FragmentThree;
import cct.buttomnavigationbar.fragment.FragmentTwo; public class MainActivity extends AppCompatActivity {
private BottomNavigationBar bottomNavigationBar;
private FragmentOne mFragmentOne;
private FragmentTwo mFragmentTwo;
private FragmentThree mFragmentThree;
private FragmentFour mFragmentFour;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BadgeItem badgeItem=new BadgeItem();
badgeItem.setHideOnSelect(false)
.setText("10").setBackgroundColor(R.color.light)
.setBorderWidth(0);
bottomNavigationBar= (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);
bottomNavigationBar.addItem(new BottomNavigationItem(R.drawable.icon_one,R.string.tab_one).setActiveColorResource(R.color.green).setBadgeItem(badgeItem))
.addItem(new BottomNavigationItem(R.drawable.icon_two, R.string.tab_two).setActiveColorResource(R.color.orange))
.addItem(new BottomNavigationItem(R.drawable.icon_three, R.string.tab_three).setActiveColorResource(R.color.pink))
.addItem(new BottomNavigationItem(R.drawable.icon_four, R.string.tab_four).setActiveColor(R.color.yellow))//依次添加item,分别icon和名称
.setFirstSelectedPosition(0)//设置默认选择item
.initialise();//初始化
bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
@Override
public void onTabSelected(int position) {
android.support.v4.app.FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
switch(position)
{
case 0:
{
if(mFragmentOne==null)
{
mFragmentOne=FragmentOne.newInstance("First Fragment");
}
transaction.replace(R.id.ll_content, mFragmentOne);
break;
}
case 1:
{
if(mFragmentTwo==null)
{
mFragmentTwo=FragmentTwo.newInstance("Second Fragment");
}
transaction.replace(R.id.ll_content,mFragmentTwo);
break;
}
case 2:
{
if(mFragmentThree==null)
{
mFragmentThree=FragmentThree.newInstance("ThirdFragment");
}
transaction.replace(R.id.ll_content,mFragmentThree);
break;
}
case 3:
{
if(mFragmentFour==null)
{
mFragmentFour=FragmentFour.newInstance("Forth Fragment");
}
transaction.replace(R.id.ll_content,mFragmentFour);
break;
}
default:
if(mFragmentOne==null)
{
mFragmentOne=FragmentOne.newInstance("First Fragment");
}
transaction.replace(R.id.ll_content,mFragmentOne);
break; }
transaction.commit();
}
@Override
public void onTabUnselected(int position) { }
@Override
public void onTabReselected(int position) { }
});
}
}

BottomNavigationBar底部导航条用法的更多相关文章

  1. 15 Flutter BottomNavigationBar自定义底部导航条 以及实现页面切换 以及模块化

    效果: /**  * Flutter  BottomNavigationBar 自定义底部导航条.以及实现页面切换:  * BottomNavigationBar是底部导航条,可以让我们定义底部Tab ...

  2. BottomNavigationBar 自定义 底部导航条

    在flutter中,BottomNavigationBar 是底部导航条,可以让我们定义底部 Tab 切换,bottomNavigationBar是 Scaffold 组件的参数. BottomNav ...

  3. tab 切换 和 BottomNavigationBar 自定义 底部导航条

    BottomNavigationBar 组件    BottomNavigationBar 是底部导航条,可以让我们定义底部 Tab 切换,bottomNavigationBar是 Scaffold ...

  4. android开发(1):底部导航条的实现 | navigation tab | activity的创建

    底部导航条,在iOS中叫tabbar,在android中叫bottombar或bottom navigation,是一个常用的切换页面的导航条. 同样,如果有良好的第三方库,我们应该优先考虑,能用好别 ...

  5. Flutter - BottomNavigationBar底部导航栏切换后,状态丢失

    如果你用过BottomNavigationBar.TabBar.还有Drawer,你就会发现,在切换页面之后,原来的页面状态就会丢失. 要是上一页有一个数据列表,很多数据,你滚动到了下头,切换页面后, ...

  6. BottomNavigationBar 底部导航控件

    BottomNavigationBar 底部导航控件 属性 说明BottomNavigationBarItem 多个 item,iconSize icon大小currentIndex 默认选中第几个o ...

  7. Android开发关闭虚拟按钮、底部导航条

    在Android开发中,遇到了一系列大大小小的问题,其中一个就是屏蔽底部实体键,我找了很多的博客也尝试了许许多多的方法,但始终不能屏蔽 HOME键,后来看见一篇博客说在Android 4.0以后,屏蔽 ...

  8. VS 2015 开发Android底部导航条----[实例代码,多图]

      1.废话背景介绍  在Build 2016开发者大会上,微软宣布,Xamarin将被整合进所有版本的Visual Studio之中. 这也就是说,Xamarin将免费提供给所有购买了Visual ...

  9. Android 修改TabLayout底部导航条Indicator的长短

    关于Tablayout,使用的应该很频繁了,但是底部导航条长短是固定死的,需要自己来改动长短,找了半天没找着方法,看了下官方建议,可以通过映射来修改自己想要的长短,其实也就几行代码的问题,看代码: p ...

随机推荐

  1. Spring Boot2.0之整合Redis

    需要的maven依赖 jar包,是对Jedis的封装 maven依赖: <project xmlns="http://maven.apache.org/POM/4.0.0" ...

  2. android 一个TextView设置多种颜色

    时候一个文本框为了强调内容需要显示不同颜色,用以下代码可以轻松实现 方法一:(适用于颜色变化多的情况)   //为文本框设置多种颜色 textView=(TextView)findViewById(R ...

  3. hiho Mission Impossible 6(模拟 未提交验证。。)

    题意:模拟文本操作 思路:模拟 #include<iostream> #include<stdio.h> #include<string.h> using name ...

  4. spark运行方式及其常用参数

    yarn cluster模式 例行任务一般会采用这种方式运行 指定固定的executor数 作业常用的参数都在其中指定了,后面的运行脚本会省略 spark-submit \ --master yarn ...

  5. 枚举子集 Codeforces306 Div2 B

    题目 分析:用二进制法去枚举子集,同时判断满足条件的子集个数加1 #include "iostream" #include "cstdio" using nam ...

  6. Android适合组件化开发的路由框架:Launch

    1.概述 最近越来越不想写代码了,特别是一些重复性的代码,比如由于每次启动一个 Activity,我们都会很习惯的在 Activity 中写下: public static void launch(A ...

  7. vue中minxin---小记

    定义全局的方法,例如定义过滤器,在很多地方都会用到,就可以定义在minxin中 demo: 数据格式化 保留指定的小数位数 var mixin={ filters:{ fixedNum:functio ...

  8. Hibernate中两种获取Session的方式

    转自:https://www.jb51.net/article/130309.htm Session:是应用程序与数据库之间的一个会话,是hibernate运作的中心,持久层操作的基础.对象的生命周期 ...

  9. 6-4 Haar特征1

    实际上特征就是图像中某个区域的像素点,经过某种四则运算之后得到的结果.所以说图像的特征它是像素经过运算之后得到的某一个结果.这个结果可以是一个具体的值,也可以是一个向量,又或是一个多维的元素.所以说特 ...

  10. liist不同遍历优缺点

    JAVA中循环删除list中元素的方法总结 印象中循环删除list中的元素使用for循环的方式是有问题的,但是可以使用增强的for循环,然后今天在使用时发现报错了,然后去科普了一下,再然后发现这是一个 ...