In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
这篇文章主要介绍"Android开发怎么快速实现底部导航栏",在日常操作中,相信很多人在Android开发怎么快速实现底部导航栏问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"Android开发怎么快速实现底部导航栏"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
Tint 着色器
优点:去除"无用"图片,节省空间
配合BottomNavigationView,实现一个快速,简洁的Tab栏
传统做法:Tab 切换,字体变色、图片变色。至少给我提供八张图,四张默认,四张选中,然后通过 selector 文件设置
现在BottomNavigationView只需四张图!!!
依赖(AndroidX)implementation 'com.google.android.material:material:1.1.0-alpha01'布局 编写渲染颜色选择器-tint_selector_menu_color menu 文件中 icon-nav_bottom_menu BottomNavigationView的点击事件
这里配合Fragmen
/* Menu显示彩色图标 */ //navBottomMenu.setItemIconTintList(null); /* 导航栏点击事件 */ navBottomMenu.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case R.id.iv_home: { FragmentManager.startFragmentHome(Fragment_A.class); return true; } case R.id.iv_wechat: { FragmentManager.startFragmentHome(Fragment_B.class); return true; } case R.id.iv_pipi: { FragmentManager.startFragmentHome(Fragment_C.class); return true; } case R.id.iv_mine: { FragmentManager.startFragmentHome(Fragment_D.class); return true; } default: break; } return false; } });配合ViewPager实现Tab栏 /* 限制页面数,防止界面反复重新加载 */ viewPager.setOffscreenPageLimit(4); // ViewPager 滑动事件监听 viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int i, float v, int i1) { } @Override public void onPageSelected(int i) { //这里我做了中间凹凸按钮,所以要特别处理以下 //如果没有我这种情况的,直接加上这个 navBottomMenu.getMenu().getItem(i).setChecked(true); 就不用再加switch语句了 switch (i) { case 0: //将滑动到的页面对应的 menu 设置为选中状态 navBottomMenu.getMenu().getItem(i).setChecked(true); break; case 1: //将滑动到的页面对应的 menu 设置为选中状态 navBottomMenu.getMenu().getItem(i).setChecked(true); break; case 2: case 3: //将滑动到的页面对应的 menu 设置为选中状态 navBottomMenu.getMenu().getItem(i + 1).setChecked(true); break; default: break; } } @Override public void onPageScrollStateChanged(int i) { } }); }对应的适配器
(仅供参考,大家也可以去参考以下别人写的代码)
public class FragPagerAdapter extends FragmentPagerAdapter { private List fragmentList; public FragPagerAdapter(@NonNull FragmentManager fm, List fragmentList) { super(fm); this.fragmentList = fragmentList; } @Override public Fragment getItem(int position) { return fragmentList.get(position); } @Override public int getCount() { return fragmentList.size(); }}到此,关于"Android开发怎么快速实现底部导航栏"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.