先来张效果图

接下来是实现过程

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. Android API中的对话框

    Android API中提供了四个Dialog的自定义子类: AlertDialog ProgressDialog DatePackerDialog TimePickerDialog 也可以派生出自己 ...

  2. java中的泛型类和泛型方法

    1.泛型是什么? 泛型(Generic type 或者 generics)是对 Java 语言的类型系统的一种扩展,以支持创建可以按类型进行参数化的类. 可以在集合框架(Collection fram ...

  3. 基于HALCON的模板匹配方法总结 (转)

    很早就想总结一下前段时间学习HALCON的心得,但由于其他的事情总是抽不出时间.去年有过一段时间的集中学习,做了许多的练习和实验,并对基于HDevelop的形状匹配算法的参数优化进行了研究,写了一篇& ...

  4. 【SOUTH CENTRAL USA 1998】 eight

    [题目链接] 点击打开链接 [算法] 这是经典的八数码问题,据说此题不做人生不完整 这里笔者用的是双向广搜,由于细节较多,笔者花了3h才通过此题 [代码] #include <algorithm ...

  5. this关键字使用

    原文地址:https://www.cnblogs.com/alsf/p/5515996.html 一,表示类中属性 1,没有使用this的情况 class Person{ // 定义Person类 p ...

  6. 杂项-公司:Aspose

    ylbtech-杂项-公司:Aspose Aspose 于2002年3月在澳大利亚悉尼创建,旗下产品覆盖文档.图表.PDF.条码.OCR.CAD.HTML.电子邮件等各个文档管理领域,为全球.NET ...

  7. gitbucket

    github固然强大,但不怎么适合企业级的开发版本管理,原因相信大家都明白,首先在github上上传和拉取代码速度是比较慢的,再者,在Github上创建一个私有仓库是收费的,那么gitbucket是一 ...

  8. 基于FBX SDK的FBX模型解析与加载 -(三)

    http://blog.csdn.net/bugrunner/article/details/7229416 6. 加载Camera和Light 在FBX模型中除了几何数据外较为常用的信息可能就是Ca ...

  9. mui中一级联动

    <!doctype html><html> <head> <meta charset="utf-8"> <title>& ...

  10. NSA互联网公开情报收集指南:迷宫中的秘密·下

    猫宁!!! 参考链接: https://www.nsa.gov/news-features/declassified-documents/assets/files/Untangling-the-Web ...