BottomNavigationView和Tablayout(3)
在上一篇的单个fragment中添加tablayout
<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" xmlns:app="http://schemas.android.com/apk/res-auto" tools:context=".BlankFragment1"> <com.google.android.material.tabs.TabLayout android:id="@+id/tablayout1" android:layout_width="match_parent" android:layout_height="40dp" android:background="@android:color/white" app:tabIndicatorColor="@android:color/darker_gray" app:layout_constraintTop_toTopOf="parent" app:tabIndicatorHeight="1dp" app:tabSelectedTextColor="@android:color/darker_gray" /> <androidx.viewpager.widget.ViewPager android:id="@+id/tab_viewpager" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_constraintTop_toBottomOf="@+id/tablayout" /> <!-- app:tabMode="scrollable" 设置tabMode属性为可以滚动 tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); tabIndicatorColor:菜单下方移动的横线的颜色 tabSelectedTextColor :菜单被选中之后的颜色 tabTextColor : 菜单正常的颜色 app:tabTextAppearance : 添加样式,这里加了样式主要是为了在文字前面加一个图所用,就是把textAllCaps设置成false --> </LinearLayout>
*/ public class BlankFragment1 extends Fragment { private Unbinder unbind; private View view; public BlankFragment1() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment // 解决点击“我的”回来方法二,首页空白的问题,推荐的方法 if (view != null) { unbind = ButterKnife.bind( this, view );//必须加,不然报ButterKnife的异常 ViewGroup parent = (ViewGroup) view.getParent(); if (parent != null) { parent.removeView(view); } return view; } view = inflater.inflate( R.layout.fragment_blank_fragment1, container, false ); unbind = ButterKnife.bind(this, view);//这里也得有,不然报ButterKnife的异常 initView(view); return view; } private void initView(View view) { ViewPager tabViewpager = view.findViewById( R.id.tab_viewpager ); TabLayout tablayout = view.findViewById( R.id.tablayout1 ); // tablayout.removeAllTabs(); // tabViewpager.removeAllViews(); tabViewpager.setAdapter( new ViewPagerAdapter2( getFragmentManager() ) ); tablayout.setupWithViewPager(tabViewpager); } }