一言不合就上图
bottomTabBar是第三方集成的fragment加radioButton和图片
最重要是导航栏上可以加上图片
老套路先注入依赖:
//bottomTabBar
compile 'com.hjm:BottomTabBar:1.1.1'
然后是布局:
<com.hjm.bottomtabbar.BottomTabBar
android:id="@+id/bottomTabBar"
android:layout_width="match_parent"
android:layout_height="match_parent"></com.hjm.bottomtabbar.BottomTabBar>
接着就是MainActivity里面代码实现:
//初始化控件
BottomTabBar btb = (BottomTabBar) findViewById(R.id.bottomTabBar);
btb.init(getSupportFragmentManager())//初始化方法布局管理
.setFontSize(8)//设置文字大小
//参数1:选中后的颜色,参数2:选中前的颜色
.setChangeColor(Color.RED, Color.DKGRAY)
//参数1:文字内容。参数2:导航图片。参数3:切换哪个fragment类
.addTabItem("首页", R.mipmap.ic_nav_home, FragmentFirstPage.class)
.addTabItem("分类", R.mipmap.ic_nav_class, FragmentFenLei.class)
.addTabItem("购物车", R.mipmap.ic_nav_cart, FragmentCart.class)
.addTabItem("我的", R.mipmap.ic_nav_user, FragmentMine.class)
//是否显示导航和上边的fragment的区分线(黑色的线太难看了一般我不喜欢在那里设)
//false为不显示那条区分线,true为显示那条区分线
.isShowDivider(false);
其中四个fragment类自己写去(注意点写完四个fragment后不用在MainActivity里面实例化)
这就完事儿了
下面是点击事件:
根据你自己的需求,爱要不要反正我不要
//点击事件
btb.setOnTabChangeListener(new BottomTabBar.OnTabChangeListener() {
@Override
public void onTabChange(int position, String name) {
switch (position)
{
case 0:
Toast.makeText(MainActivity.this,"first page",Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(MainActivity.this,"fen lei",Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(MainActivity.this,"cart",Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(MainActivity.this,"mine",Toast.LENGTH_SHORT).show();
break;
}
}
});
强调1点:
1我的Android studio是2.3.3版本,SDK用的v7…..26.+的