BottomNavigationBar底部导航条用法
先来张效果图

接下来是实现过程
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底部导航条用法的更多相关文章
- 15 Flutter BottomNavigationBar自定义底部导航条 以及实现页面切换 以及模块化
效果: /** * Flutter BottomNavigationBar 自定义底部导航条.以及实现页面切换: * BottomNavigationBar是底部导航条,可以让我们定义底部Tab ...
- BottomNavigationBar 自定义 底部导航条
在flutter中,BottomNavigationBar 是底部导航条,可以让我们定义底部 Tab 切换,bottomNavigationBar是 Scaffold 组件的参数. BottomNav ...
- tab 切换 和 BottomNavigationBar 自定义 底部导航条
BottomNavigationBar 组件 BottomNavigationBar 是底部导航条,可以让我们定义底部 Tab 切换,bottomNavigationBar是 Scaffold ...
- android开发(1):底部导航条的实现 | navigation tab | activity的创建
底部导航条,在iOS中叫tabbar,在android中叫bottombar或bottom navigation,是一个常用的切换页面的导航条. 同样,如果有良好的第三方库,我们应该优先考虑,能用好别 ...
- Flutter - BottomNavigationBar底部导航栏切换后,状态丢失
如果你用过BottomNavigationBar.TabBar.还有Drawer,你就会发现,在切换页面之后,原来的页面状态就会丢失. 要是上一页有一个数据列表,很多数据,你滚动到了下头,切换页面后, ...
- BottomNavigationBar 底部导航控件
BottomNavigationBar 底部导航控件 属性 说明BottomNavigationBarItem 多个 item,iconSize icon大小currentIndex 默认选中第几个o ...
- Android开发关闭虚拟按钮、底部导航条
在Android开发中,遇到了一系列大大小小的问题,其中一个就是屏蔽底部实体键,我找了很多的博客也尝试了许许多多的方法,但始终不能屏蔽 HOME键,后来看见一篇博客说在Android 4.0以后,屏蔽 ...
- VS 2015 开发Android底部导航条----[实例代码,多图]
1.废话背景介绍 在Build 2016开发者大会上,微软宣布,Xamarin将被整合进所有版本的Visual Studio之中. 这也就是说,Xamarin将免费提供给所有购买了Visual ...
- Android 修改TabLayout底部导航条Indicator的长短
关于Tablayout,使用的应该很频繁了,但是底部导航条长短是固定死的,需要自己来改动长短,找了半天没找着方法,看了下官方建议,可以通过映射来修改自己想要的长短,其实也就几行代码的问题,看代码: p ...
随机推荐
- Jackson 对象与json数据互转工具类JacksonUtil
1,User对象 package com.st.json; import java.util.Date; /** * @Description: JSON序列化和反序列化使用的User类 * @aut ...
- JQuery验证成功之后,使用ajax提交数据
function checkForm(){ validator = $("#commentForm").validate({// #formId为需要进行验证的表单ID error ...
- pyspark 日期格式
1. 获取当前日期 from pyspark.sql.functions import current_date spark.range(3).withColumn('date',current_da ...
- Vue安装及插件Vue Devtools
vue安装: # 最新稳定版 $ npm install vue # 全局安装 vue-cli $ npm install --global vue-cli # 创建一个基于 webpack 模板的新 ...
- codeforces 696B B. Puzzles(树形dp+概率)
题目链接: B. Puzzles time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- CodeForces-607B:Zuma (基础区间DP)
Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the ...
- wsdl 生成代码 WCF
具体方法: 打开Microsoft Visual Studio 2008->Visual Studio Tools->Visual Studio 2008 命令提示窗口. 输入:wsdl ...
- 国外1.5免费空间000webhost申请方法
空间大小:1500M 支持语言:PHP 数 据 库:MYSQL 国家/地区:国外 申请地址:http://www.000webhost.com/ 1500M/100GB/PHP/MYSQL/FTP ...
- cocos2d-x 坐标系解惑
1.CCTouch* touch->getLocation() ---- 返回当前触摸点在openGL坐标系中的位置 openGL坐标系,原点在左下角,x向右为正,y向上为正. 2.CCTouc ...
- centos查看CPU的数量
# 总核数 = 物理CPU个数 X 每颗物理CPU的核数 # 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数 # 查看物理CPU个数 cat /proc/cpuinfo| ...