首先主要实现的功能就BottomBar的底部导航功能,还有Fragment的联动,如上图。
首先我们先导依赖
compile 'com.roughike:bottom-bar:2.3.1'
然后在我们的res文件夹下,新建一个xml文件,写上我们底部要实现的图片和文字
<?xml version="1.0" encoding="utf-8"?>
<tabs>
<tab
id="@+id/tab_yi"
icon="@mipmap/found"
title="Recents"
barColorWhenSelected="#FE3D81" />
<tab
id="@+id/tab_er"
title="Favorites"
icon="@mipmap/fancy"
barColorWhenSelected="#5D3C35" />
<tab
id="@+id/tab_san"
icon="@mipmap/special"
title="Nearby"
barColorWhenSelected="#7B1FA2" />
<tab
id="@+id/tab_si"
icon="@mipmap/my"
title="Restaurants"
barColorWhenSelected="#FF9800" />
</tabs>
这是我的们布局里的内容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.xinyun.mybar.MainActivity">
<FrameLayout
android:id="@+id/contentContainer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<com.roughike.bottombar.BottomBar
android:id="@+id/bottomBar"
android:layout_width="match_parent"
android:layout_height="56dp"
app:bb_tabXmlResource="@xml/bottombar_menu"/>
</LinearLayout>
最后就是我们的代码了
public class MainActivity extends AppCompatActivity {
public BottomBar bottomBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//去掉头部
getSupportActionBar().hide();
//找控件
bottomBar = findViewById(R.id.bottomBar);
//Fragment关联
bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
@Override
public void onTabSelected(int tabId) {
Object ob=null;
switch (tabId){
case R.id.tab_yi:
ob = new FragmentOne();
break;
case R.id.tab_er:
ob = new FragmentEr();
break;
case R.id.tab_san:
ob = new FragmentSan();
break;
case R.id.tab_si:
ob = new FragmentSi();
break;
default:
break;
}getSupportFragmentManager().beginTransaction().replace(R.id.contentContainer,(Fragment) ob).commit();
}
});
}
}