首先在android studio中加入
compile 'com.ashokvarma.android:bottom-navigation-bar:1.3.0'
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="com.feiyiqi.gut.MainActivity">
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.ashokvarma.bottomnavigation.BottomNavigationBar
android:id="@+id/bottom_navigation_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private List<Fragment> fragments;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragments = new ArrayList<>();
initView();
}
/*
初始化视图
*/
public void initView(){
BottomNavigationBar bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);
//设置BottomNavigation模式
bottomNavigationBar.setMode(BottomNavigationBar.MODE_DEFAULT);
//设置BottomNavigation背景风格
bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
//添加导航选项
bottomNavigationBar.addItem(new BottomNavigationItem(R.mipmap.ic_bottom_bar_home,"首页")
.setActiveColorResource(R.color.colorPrimary))
.addItem(new BottomNavigationItem(R.mipmap.ic_bottom_bar_application,"应用")
.setActiveColorResource(R.color.colorPrimary))
.addItem(new BottomNavigationItem(R.mipmap.ic_bottom_bar_my,"我的")
.setActiveColorResource(R.color.colorPrimary))
.initialise();
}
/**
* 设置默认显示的fragment
*/
private void setDefaultFragment() {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.id_content, HomeFragment.newInstance("首页"));
transaction.commit();
}
/**
* 获取fragment
*
* @return fragment列表
*/
private List<Fragment> getFragments() {
List<Fragment> fragments = new ArrayList<>();
fragments.add(HomeFragment.newInstance("首页"));
fragments.add(FindFragmente.newInstance("应用"));
fragments.add(MineFragment.newInstance("我的"));
return fragments;
}
/*
*设置切换Fragment
*/
public void onTabSelected(int position) {
if (fragments != null) {
if (position < fragments.size()) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Fragment fragment = fragments.get(position);
ft.replace(R.id.id_content, fragment);
ft.commitAllowingStateLoss();//选择性的提交,和commit有一定的区别,他不保证数据完整传输
}
}
}
public void onTabUnselected(int position) {
}
public void onTabReselected(int position) {
}
}