先来张效果图

接下来是实现过程

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. IntelliJ IDEA 2017 反向代理工具新方法激活

    来源:http://blog.lanyus.com/archives/317.html 反向代理工具, 可用于激活JRebel  (win64) 1.点击进入 https://github.com/i ...

  2. linux初级学习笔记一:linux操作系统及常用命令,及如何获取命令的使用帮助!(视频序号:02_1,2)

    本节学习的命令:ls,cd,type,pwd, printenv, hash, date, clock, man, hwclock, info, cal, echo, printf, file! 本节 ...

  3. java的数字精确计算问题-BigDecimal

    java的数字运算,偶尔会出现精度的问题,以下阐述的 java的BigDecimal类的使用. 例如: System.out.println(0.9+0.3); 结果1.2 System.out.pr ...

  4. 【重要】Selenium2+python自动化44-元素定位参数化(find_element)

    转:https://www.cnblogs.com/yoyoketang/p/6551274.html 前言 元素定位有八种方法,这个能看到这一篇的小伙伴都知道了,那么有没有一种方法,可以把八种定位合 ...

  5. 任务24:WebHost的配置

    24 任务24:WebHost的配置 创建HelloCore的项目 我们新建一个空的mvc项目 我们在这里调用COnfigureAppConfiguration方法更改默认的配置.为读取setting ...

  6. 算法练习--LeetCode--29. Divide Two Integers

    Divide Two Integers Given two integers dividend and divisor, divide two integers without using multi ...

  7. Luogu P1156 垃圾陷阱 【dp】By cellur925

    题目传送门 这题...看上去浓浓的背包气息...但是并不好设计状态啊emmm. 我们考虑可能成为状态的量:高度.血量.时间.物品.看数据范围也猜到应该大概是个二维dp了w. 正确的状态设计之一:设$f ...

  8. Apache-kylin-2.0.0-bin-hbase1x.tar.gz的下载与安装(图文详解)

    首先,对于Apache Kylin的安装,我有话要说. 由于Apache Kylin本身只是一个Server,所以安装部署还是比较简单的.但是它的前提要求是Hadoop.Hive.HBase必须已经安 ...

  9. Hadoop Hive概念学习系列之HDFS、Hive、MySQL、Sqoop之间的数据导入导出(强烈建议去看)

    Hive总结(七)Hive四种数据导入方式 (强烈建议去看) Hive几种数据导出方式 https://www.iteblog.com/archives/955 (强烈建议去看) 把MySQL里的数据 ...

  10. byte的范围-128-127

    01111111  表示的是最大的数字 是127这个没有问题  ,前面的0 表示的正数,1表示的负数 而负数在计算机中的存储都是通过补码的形式存在的,也就是说 1 1111 111 是计算机中最小的数 ...