怎么在Android中使用TabLayout实现一个底部Tab-创新互联
本篇文章为大家展示了怎么在Android中使用TabLayout 实现一个底部Tab,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
在上街等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站建设、网站制作 网站设计制作按需定制网站,公司网站建设,企业网站建设,品牌网站设计,全网营销推广,外贸网站建设,上街网站建设费用合理。代码:
public class CustomBotTabItem { private TabLayout mTabLayout; private ViewPager mViewPager; private Context mContext; //底部Tab标题 private final String[] mTitles = {"主页", "理财", "添加", "消息", "我的"}; //返回CustomBotTabItem实例 public static CustomBotTabItem create() { return TabItemHolder.sCustomTabItem; } //创建CustomBotTabItem实例 private static class TabItemHolder { private static CustomBotTabItem sCustomTabItem = new CustomBotTabItem(); } //引入布局需要的Context public CustomBotTabItem setContext(Context context) { mContext = context; return this; } //需要自定义的TabLayout public CustomBotTabItem setTabLayout(TabLayout tabLayout) { mTabLayout = tabLayout; return this; } //设置与TabLayout关联的ViewPager public CustomBotTabItem setViewPager(ViewPager viewPager) { mViewPager = viewPager; return this; } //创建Tab public CustomBotTabItem build() { initTabLayout(); return this; } //初始化Tab private void initTabLayout() { mTabLayout.setupWithViewPager(mViewPager); //第二个参数为selector,下同 mTabLayout.getTabAt(0).setCustomView(getTabView(0, R.drawable.home_icon_selector)); mTabLayout.getTabAt(1).setCustomView(getTabView(1, R.drawable.manage_icon_selector)); mTabLayout.getTabAt(2).setCustomView(getTabView(2, R.drawable.add_icon_selector)); mTabLayout.getTabAt(3).setCustomView(getTabView(3, R.drawable.find_icon_selector)); mTabLayout.getTabAt(4).setCustomView(getTabView(4, R.drawable.money_icon_selector)); tabSelectListener(); } //自定义Tab样式 private View getTabView(final int position, int resId) { final View view = LayoutInflater.from(mContext).inflate(R.layout.bottom_tab_item, null); TextView tvTitle = (TextView) view.findViewById(R.id.id_tv_title); final ImageView ivTitle = (ImageView) view.findViewById(R.id.id_iv_title); ivTitle.setImageResource(resId); tvTitle.setText(mTitles[position]); //默认第一个tab选中,设置字体为选中色 if (position == 0) { tvTitle.setTextColor(Color.parseColor("#4192e3")); } else { tvTitle.setTextColor(Color.parseColor("#262a3b")); } //点击Tab切换 view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mViewPager.setCurrentItem(position); } }); return view; } //Tab监听 private void tabSelectListener() { mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { changeTabStatus(tab, true); } @Override public void onTabUnselected(TabLayout.Tab tab) { changeTabStatus(tab, false); } @Override public void onTabReselected(TabLayout.Tab tab) { } }); } //切换Tab文字是否选中的的颜色 private void changeTabStatus(TabLayout.Tab tab, boolean selected) { View view = tab.getCustomView(); if (view == null) { return; } TextView tvTitle = (TextView) view.findViewById(R.id.id_tv_title); if (selected) { tvTitle.setTextColor(Color.parseColor("#4192e3")); } else { tvTitle.setTextColor(Color.parseColor("#262a3b")); } } }
Activity 代码:
public class BottomTabLayoutActivity extends AppCompatActivity { private TabLayout mTabLayout; private ViewPager mViewPager; private ListmFragmentList; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_bot_tab_layout); findView(); initFragmentList(); setVpAdapter(); CustomBotTabItem item = CustomBotTabItem.create(); item.setContext(this) .setViewPager(mViewPager) .setTabLayout(mTabLayout) .build(); } //findViewById private void findView() { mTabLayout = (TabLayout) findViewById(R.id.id_tab_layout); mViewPager = (ViewPager) findViewById(R.id.id_vp); } //初始化需要的Fragment private void initFragmentList() { mFragmentList = new ArrayList<>(); mFragmentList.add(TabFragment.getInstance("主页")); mFragmentList.add(TabFragment.getInstance("理财")); mFragmentList.add(TabFragment.getInstance("添加")); mFragmentList.add(TabFragment.getInstance("消息")); mFragmentList.add(TabFragment.getInstance("我的")); } //设置ViewPager的Adapter private void setVpAdapter() { mViewPager.setAdapter(new FragmentAdapter(getSupportFragmentManager(), mFragmentList, this)); } }
Activity.xml
上述内容就是怎么在Android中使用TabLayout 实现一个底部Tab,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。
文章题目:怎么在Android中使用TabLayout实现一个底部Tab-创新互联
文章分享:http://scyanting.com/article/jdded.html