1、佈局使用 <HorizontalScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="none"> <com.flyco.tablayout.CommonTabLayout android:id="@+id/smart_action_table1" android:layout_width="match_parent" android:layout_height="@dimen/base40dp" app:tl_divider_color="@android:color/transparent" app:tl_divider_width="0dp" app:tl_indicator_color="@color/colorAccent" app:tl_indicator_corner_radius="@dimen/base5dp" app:tl_indicator_height="@dimen/base4dp" app:tl_indicator_width="@dimen/base25dp" app:tl_tab_padding="@dimen/base15dp" app:tl_tab_space_equal="false" app:tl_textBold="BOTH" app:tl_textSelectColor="@color/colorText" app:tl_textUnselectColor="@color/colorText" app:tl_textsize="@dimen/base13sp" app:tl_underline_color="@color/colorLine" app:tl_underline_height="1px" /> </HorizontalScrollView> <androidx.viewpager.widget.ViewPager android:id="@+id/view_pager_action" android:layout_width="match_parent" android:layout_height="match_parent" />
注:看佈局或許你們會疑問爲什麼我要在外面加上HorizontalScrollView,加HorizontalScrollView還不如去用SlidingTabLayout,
CommonTabLayout這個可塑性比較好,個人覺得
2、JAVA代碼如下
private List<CustomTabEntity> list=new ArrayList<>();//集合,用來放整體數據 private List<String> list1=new ArrayList<>();//集合,用來放整體數據
list1.add("測試一"); list1.add("測試二"); list1.add("測試三"); list1.add("測試四"); list1.add("測試五"); list1.add("測試六"); list1.add("測試七"); list1.add("測試八"); list1.add("測試九"); list1.add("測試十"); for(int i =0;i<list1.size();i++){ final int j = i; list.add(new CustomTabEntity() { @Override public String getTabTitle() { return list1.get(j); } @Override public int getTabSelectedIcon() { return 0; } @Override public int getTabUnselectedIcon() { return 0; } }); } smart_action_table1.setTabData((ArrayList<CustomTabEntity>)list); smart_action_table1.setOnTabSelectListener(new OnTabSelectListener() { @Override public void onTabSelect(int position) { ToastUtils.showShort(list.get(position).getTabTitle()); } @Override public void onTabReselect(int position) { } });