BottomNavigationBar(Materia-Design风格)
基于android的底部导航栏,Google给出了一个统一的设计风格标准,BottomNavigationBar。根据该设计风格,可以很好的实现美观的底部导航栏
设计规范:
https://material.io/guidelines/components/bottom-navigation.html#bottom-navigation-specs (官方)
http://blog.csdn.net/ddwhan0123/article/details/50921450
使用详解:
添加依赖
在module的gradle中
compile 'com.ashokvarma.android:bottom-navigation-bar:1.4.3'
在Activity的XML中
RelativeLayout布局下
<com.ashokvarma.bottomnavigation.BottomNavigationBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="@+id/bottom_navigation_bar"
/>
java类中
创建一个initBottomView的方法:
onCreate中去调用该方法
initBottomView();
再在 initBottomView()方法体中:
// 先声明
BottomNavigationBar bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);
再根据该Bottom对象
模式跟背景的设置都要在添加tab前面,不然不会有效果
bottomNavigationBar.
// 设置模式
// MODE_FIXED 未选中item会显示文字 但不会有切换动画
// MODE_SHIFTING 未选中的Item不会显示文字,选中的会显示文字 切换时会有动画
.setMode(MODE_FIXED)
// 设置导航栏背景风格
//BACKGROUND_STYLE_STATIC 为点击无水波
//BACKGROUND_STYLE_RIPPLE为点击有水波
.setBackgroundStyle(BACKGROUND_STYLE_RIPPLE)
// 在new中的图片 表示选中时的图片
.addItem(new BottomNavigationItem(R.mipmap.tabbar_crm_selected2x, "CRM")
//在BACKGROUND_STYLE_STATIC下 表示选中了的item的颜色 见下图1
//在BACKGROUND_STYLE_RIPPLE下,表示整个容器的背景色 见下图2
.setActiveColorResource(R.color.orange)
//非选中时的图片
.setInactiveIcon(ContextCompat.getDrawable(this,R.mipmap.tabbar_crm2x))
)
...
...
// 设置底部点击的位置
.setFirstSelectedPosition(FIRSTSELECTEDPOSITION )
//所有的设置需在调用该方法前完成
.initialise();
注意:这是要使用R.color 使用的是setActiveColorResource 而不是setActiveColor!!!!!!
图1
BACKGROUND_STYLE_STATIC下 设置 .setActiveColorResource(R.color.colorPrimary)
表示选中了的item的颜色
图2
BACKGROUND_STYLE_RIPPLE下 设置 .setActiveColorResource(R.color.colorPrimary)
表示整个容器的背景色
并且若设置了不同item的背景颜色 会有颜色切换的水波效果
写底部栏的点击事件监听
将Activity类
implements BottomNavigationBar.OnTabSelectedListener
这样就可以在initView的时候只进行不需要写匿名内部类:
// 添加底部栏的点击事件监听 如根据position的条目选择 不选择 再选择
bottomNavigationBar.setTabSelectedListener(this);
再将 相应的 重写方法 放在 外边
@Override
public void onTabSelected(int position) {
Log.i("position", String.valueOf(position));
}
@Override
public void onTabUnselected(int position) {
}
@Override
public void onTabReselected(int position) {
}
与Fragment关联
首先,要创建fragment (具体参考http://blog.csdn.net/weixin_37577039/article/details/79056855)
注意:这里的创建的fragment继承的Fragment要为
import android.app.Fragment;
app形式的 而不是
import android.support.v4.app.Fragment;
形式
否则后面的 replace 会识别错误
使用:
首先,设置默认的fragment
initBottomView()中
...
...
.initialise();//所有的设置需在调用该方法前完成
setDefaultFragment();
然后 setDefaultFragment()方法体中:
private void setDefaultFragment() {
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
fragment2 = Fragment2.newInstance("战斗台","11"); // 用于传值给fragment
transaction.replace(R.id.ll_1, fragment2);
transaction.commit();
}
再在点击item事件中:
@Override
public void onTabSelected(int position) {
// 创建Fragment
FragmentManager fm = this.getFragmentManager();
//开启事务
FragmentTransaction transaction = fm.beginTransaction();
switch (position) {
case 0:
if (fragment0 == null) {
fragment0 = Fragment0.newInstance("CRM","11");
}
transaction.replace(R.id.ll_1, fragment0);
break;
case 1:
if (fragment1 == null) {
fragment1 = Fragment1.newInstance("OA","11");
}
transaction.replace(R.id.ll_1, fragment1);
break;
case 2:
if (fragment2 == null) {
fragment2 = Fragment2.newInstance("战斗台","11");
}
transaction.replace(R.id.ll_1, fragment2);
break;
case 3:
if (fragment3 == null) {
fragment3 = Fragment3.newInstance("数据盘","11");
}
transaction.replace(R.id.ll_1, fragment3);
break;
case 4:
if (fragment4 == null) {
fragment4 = Fragment4.newInstance("我的","11");
}
transaction.replace(R.id.ll_1, fragment4);
break;
default:
break;
}
// 事务提交
transaction.commit();
}
跳回Fragment中:
一般可以直接Finish掉当前界面即可回跳到之前的Fragment
但是若想跳转到其他的Fragment:
根据intent的回传跳转到不同的Fragment界面中:
// 设置一个默认的 选择值
private int FIRSTSELECTEDPOSITION=2;
// 获取回传值
Intent intent =getIntent();
FIRSTSELECTEDPOSITION=intent.getIntExtra("flag",2);//后一个数值为 前一个不存在时 指定的一个默认值
// 在bottomNavigationBar中设置
.setFirstSelectedPosition(FIRSTSELECTEDPOSITION)// 设置底部点击的初始位置
...
...
未完待填
常见问题:
1 出现 资源 color/accentcolor 找不到
在color.xml文件中 加入
//自己设置一个初始颜色 这里设置为白色
<color name="colorAccent">#FFFFFF</color>