Android顶部导航
16lz
2021-01-24
安卓顶部导航使用的是TabLayout和ViewPager的结合,布局代码如下:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/shape_gradient"> <android.support.design.widget.TabLayout android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:background="@drawable/shape_gradient" app:tabIndicatorColor="#2b86c5" app:tabSelectedTextColor="#FFFFFF" /> </android.support.design.widget.AppBarLayout> <android.support.v4.view.ViewPager android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /></android.support.design.widget.CoordinatorLayout>
实现代码,Fragment不懂得可以自己了解:
private List<Fragment> fragList= new ArrayList<>();//每个页面的fragment页面 fragList.add(new SecondFragmentFirst()); fragList.add(new SecondFragmentSecond()); fragList.add(new SecondFragmentThird()); TabLayout tabs = view.findViewById(R.id.tabs); ViewPager viewPager = view.findViewById(R.id.view_pager); //设置最大缓存界面 viewPager.setOffscreenPageLimit(3); viewPager.setAdapter(new ViewPageAdapter(getChildFragmentManager())); //将viewpage添加到TabLayout中 tabs.setupWithViewPager(viewPager); for (int i = 0; i < tabs.getTabCount(); i++) { TabLayout.Tab tab = tabs.getTabAt(i); if (tab != null) { tab.setCustomView(getTabView(i)); } } tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { //在这里可以设置选中状态下 tab字体显示样式 viewPager.setCurrentItem(tab.getPosition()); View view = tab.getCustomView(); //判断其左边对象是否为其右边类的实例 if (view instanceof TextView) { ((TextView) view).setTextSize(19); ((TextView) view).setTextColor(ContextCompat.getColor(getActivity(), R.color.orange)); } } @Override public void onTabUnselected(TabLayout.Tab tab) { View view = tab.getCustomView(); if (view instanceof TextView) { ((TextView) view).setTextSize(14); ((TextView) view).setTextColor(ContextCompat.getColor(getActivity(), R.color.white)); } } @Override public void onTabReselected(TabLayout.Tab tab) { } }); return view; } /** * 自定义Tab的View * * @param currentPosition * @return */ private View getTabView(int currentPosition) { View view = LayoutInflater.from(getActivity()).inflate(R.layout.public_tab_iterm, null); TextView textView = view.findViewById(R.id.tab_item_textview); textView.setText(titleList.get(currentPosition)); return view; } class ViewPageAdapter extends FragmentPagerAdapter { public ViewPageAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int i) { return fragList.get(i); } @Override public int getCount() { return titleList.size(); } }
更多相关文章
- Android属性动画源代码解析(超详细)
- android 完美的ListView实现【原创】
- android中置调用指定浏览器
- Android获得SD卡剩余容量的源码
- android 随手记 videoview循环播放网络视频 和mediaplayer+sufac
- 系出名门Android(6) - 控件(View)之DatePicker, TimePicker, Tog
- android上传图片或文件
- android 震动的api介绍
- android 嵌套h5问题集锦