在project下的build.grade中添加依賴 compile 'com.ashokvarma.android:bottom-navigation-bar:0.9.5' 2.在主佈局文件中加入代碼 <LinearLayout android:id="@+id/tb" android:layout_below="@id/title" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" />android
<!--利用BottomNavigationBar來顯示導航欄-->ide
<com.ashokvarma.bottomnavigation.BottomNavigationBar android:id="@+id/bottom_navigation_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true"/> 3.在主Activity中的onCreate方法中添加以下邏輯 bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar); bottomNavigationBar .addItem(new BottomNavigationItem(R.mipmap.main_nav_home, "主頁").setActiveColor(R.color.colorTheme)) .addItem(new BottomNavigationItem(R.mipmap.main_nav_water, "水").setActiveColor(R.color.colorTheme)) .addItem(new BottomNavigationItem(R.mipmap.main_nav_electric, "電").setActiveColor(R.color.colorTheme)) .addItem(new BottomNavigationItem(R.mipmap.main_nav_gas, "燃氣").setActiveColor(R.color.colorTheme)) .addItem(new BottomNavigationItem(R.mipmap.main_nav_heat, "熱力").setActiveColor(R.color.colorTheme)) .setFirstSelectedPosition(lastSelectedPosition) .initialise(); bottomNavigationBar.setTabSelectedListener(this); setDefaultFragment(); 4.在主Activity中添加以下方法 /**佈局
@Override public void onTabSelected(int position) { Log.d(TAG, "onTabSelected() called with: " + "position = [" + position + "]"); FragmentManager fm = this.getFragmentManager(); //開啓事務 FragmentTransaction transaction = fm.beginTransaction(); switch (position) { case 0: if (mHomeFragment == null) { mHomeFragment = HomeFragment.newInstance(); } transaction.replace(R.id.tb, mHomeFragment); break; case 1: if (mWaterFragment == null) { mWaterFragment = WaterFragment.newInstance(); } transaction.replace(R.id.tb, mWaterFragment); break; case 2: if (mElectricFragment == null) { mElectricFragment = ElectricFragment.newInstance(); } transaction.replace(R.id.tb, mElectricFragment); break; case 3: if (mGasFragment == null) { mGasFragment = GasFragment.newInstance(); } transaction.replace(R.id.tb, mGasFragment); break; case 4: if (mHeatFragment == null) { mHeatFragment = HeatFragment.newInstance(); } transaction.replace(R.id.tb, mHeatFragment); break; default: break; } // 事務提交 transaction.commit(); }ui
@Override public void onTabUnselected(int position) { Log.d(TAG, "onTabUnselected() called with: " + "position = [" + position + "]"); }this
@Override public void onTabReselected(int position) {事務
} 5.建立幾個Fragment(這裏就不寫了,隨便建立都行)ip