本文主要是介绍android BottomNavigationView实现导航栏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.布局
<android.support.design.widget.BottomNavigationViewandroid:id="@+id/design_navigation_view"android:layout_width="match_parent"android:layout_height="wrap_content"app:itemBackground="@color/white"app:itemIconTint="@color/selector"app:itemTextColor="@color/selector"app:menu="@menu/bottom_menu" />
2.selector
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:color="@color/colorAccent" android:state_checked="true" /><item android:color="@color/colorPrimary" android:state_checked="false" />
</selector>
3.menu
<menu xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:id="@+id/menu_left"android:icon="@mipmap/home"app:showAsAction="always"android:title="左" /><itemandroid:id="@+id/menu_center"android:icon="@mipmap/user"app:showAsAction="always"android:title="中" /><itemandroid:id="@+id/menu_right"android:icon="@mipmap/home"app:showAsAction="always"android:title="右" />
</menu>
4.点击导航栏切换viewpager
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {@Overridepublic boolean onNavigationItemSelected(@NonNull MenuItem item) {switch (item.getItemId()) {case R.id.menu_left:viewPager.setCurrentItem(0);break;case R.id.menu_center:viewPager.setCurrentItem(1);break;case R.id.menu_right:viewPager.setCurrentItem(2);break;default:break;}return false;}});
5.viewpager切换联动导航栏
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {private MenuItem menuItem;@Overridepublic void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}@Overridepublic void onPageSelected(int position) {if (menuItem != null) {menuItem.setChecked(false);} else {bottomNavigationView.getMenu().getItem(0).setChecked(false);}menuItem = bottomNavigationView.getMenu().getItem(position);menuItem.setChecked(true);}@Overridepublic void onPageScrollStateChanged(int state) {}});
这篇关于android BottomNavigationView实现导航栏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!