// 布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" <FrameLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:id="@+id/frame_content"></FrameLayout> <com.ashokvarma.bottomnavigation.BottomNavigationBar android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:elevation="5dp" android:id="@+id/bnb_main"> </com.ashokvarma.bottomnavigation.BottomNavigationBar> </LinearLayout>
//代码
public class MainActivity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener { private BottomNavigationBar mBottomNavigationBar; private FragmentTransaction mTransaction; private FragmentOne mPlanFragment; private Fragment mCurrentFragment; private FragmentTwo mAccountFragment; private FragmentThree mNewsFragment; private FragmentFore mLifeFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 底部导航栏 mBottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bnb_main); mBottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED); mBottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC); mBottomNavigationBar.addItem(new BottomNavigationItem(R.mipmap.ic_launcher,"规划") .setActiveColorResource(R.color.colorPrimaryDark)) .addItem(new BottomNavigationItem(R.mipmap.ic_launcher,"记账") .setActiveColorResource(R.color.colorPrimary)) .addItem(new BottomNavigationItem(R.mipmap.ic_launcher,"新闻") .setActiveColorResource(R.color.colorAccent)) .addItem(new BottomNavigationItem(R.mipmap.ic_launcher,"生活") .setActiveColorResource(R.color.colorAccent)) .setFirstSelectedPosition(0) .initialise(); // 设置默认页 mTransaction = getSupportFragmentManager().beginTransaction(); mPlanFragment = new FragmentOne(); mTransaction.add(R.id.frame_content, mPlanFragment).commit(); mCurrentFragment = mPlanFragment; // 设置导航监听器 mBottomNavigationBar.setTabSelectedListener(this); } @Override public void onTabSelected(int position) { switch (position) { case 0: if (mPlanFragment == null) { mPlanFragment = new FragmentOne(); } switchFragment(mPlanFragment); break; case 1: if (mAccountFragment == null) { mAccountFragment = new FragmentTwo(); } switchFragment(mAccountFragment); break; case 2: if (mNewsFragment == null) { mNewsFragment = new FragmentThree(); } switchFragment(mNewsFragment); break; case 3: if (mLifeFragment == null) { mLifeFragment = new FragmentFore(); } switchFragment(mLifeFragment); break; default: break; } } @Override public void onTabUnselected(int position) { } @Override public void onTabReselected(int position) { } /** * 导航切换 */ private void switchFragment(Fragment fragment) { if (mCurrentFragment != fragment) { mTransaction = getSupportFragmentManager().beginTransaction(); if (!fragment.isAdded()) { mTransaction.hide(mCurrentFragment).add(R.id.frame_content, fragment).commit(); } else { mTransaction.hide(mCurrentFragment).show(fragment).commit(); } mCurrentFragment = fragment; } } }
cardview
最新推荐文章于 2024-08-19 10:32:35 发布